实体
// Lombok annotations
public class Plan {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(updatable = false, nullable = false)
private UUID id;
private LocalDateTime createdAt;
@OneToMany
@JoinColumn
private Set<AbsractTask> abstractTasks = new TreeSet<>();
}
// Lombok annotations
public class AbstractTask implements Comparable<AbstractTask> {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(updatable = false, nullable = false)
private UUID id;
@Column(name = "type", insertable = false, updatable = false)
protected String type = this.getClass().getSimpleName().toLowerCase();
private LocalDateTime createdAt;
private Integer customOrder;
}
// Lombok annotation
public class TextTask extends AbstractTask {
private String text;
private Boolean completed;
}
// Lombok annotations
public class NumericalTask extends AbstractTask {
int current;
int target;
}
DTO公司
// Lombok annotations
public class PlanDto {
private UUID id;
private LocalDateTime createdAt;
private Set<AbstractTaskDto> tasks;
}
// Lombok annotations
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
property = "type")
@JsonSubTypes({
@Type(value = TextTaskDto.class),
@Type(value = NumericalTaskDto.class)})
public class AbstractTaskDto {
private UUID id;
protected String type = this.getClass().getSimpleName().toLowerCase();
private LocalDateTime createdAt;
private Integer customOrder;
// Lombok annotations
public class TextTaskDto extends AbstractTaskDto {
private String text;
private Boolean completed;
}
// Lombok annotations
public class NumericalTaskDto extends AbstractTaskDto {
private int current;
private int target;
}
制图器
@AllArgsConstructor
public class PlanMapper {
private final static ModelMapper modelMapper = new ModelMapper();
public static PlanDto convertToDto(Plan plan) {
modelMapper.typeMap(Plan.class, PlanDto.class);
modelMapper.getConfiguration().setPropertyCondition(Conditions.isNotNull());
return modelMapper.map(plan, planDto.class);
}
}
发生什么事了
我将jackson与spring boot结合使用,最终,我希望看到我的控制器对一段时间的json响应 Plan
对象类似于:
{
"id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"createdAt": "2021-01-04T07:38:53.362Z",
"tasks": [
{
"id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"type": "textTask",
"createdAt": "2021-01-04T07:38:53.362Z",
"customOrder": 1,
"text": "string",
"completed": false
},
{
"id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"type": "numericalTask",
"createdAt": "2021-01-04T07:38:53.362Z",
"customOrder": 2,
"current": 0,
"target": 5
}
]
}
也就是说,在相应的json列表中存在各种子类型(api的使用者可以使用 type
字段作为 predicate 来确定如何处理元素)。然而,我看到的是json列表(和dto)的元素 Set
简化为抽象的父对象。例如
{
"id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"createdAt": "2021-01-04T07:38:53.362Z",
"tasks": [
{
"id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"type": "textTask",
"createdAt": "2021-01-04T07:38:53.362Z",
"customOrder": 1
},
{
"id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"type": "numericalTask",
"createdAt": "2021-01-04T07:38:53.362Z",
"customOrder": 2
}
]
}
对于包含抽象元素集合(也有相应的dto类)的实体,是否有方法配置modelmapper以在从实体到dto的Map过程中维护特定于子级的字段?
暂无答案!
目前还没有任何答案,快来回答吧!