假设我有一个实体:
@Entity
@Table(name = "history_experience_entity")
@AllArgsConstructor
@NoArgsConstructor
public class HistoryExperienceEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "history_start_date")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd.MM.yyyy")
private LocalDate historyStartDate;
@Column(name = "history_end_date")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd.MM.yyyy")
private LocalDate historyEndDate;
@Column(name = "is_working")
private Boolean isWorking;
..........
}
我使用 JodaLocalDate
并为两个字段设置格式,例如"20.01.2017"
. 我通过邮递员发送 POST 请求:
{
"historyStartDate": "15.03.2016",
"historyEndDate": "30.03.2017",
"isWorking": true
}
这没有问题。但是,当我有一个拥有该实体集合的 DTO 时:
private Set<HistoryExperienceEntity> historyExperienceEntities;
我尝试这个 POST 请求:
"historyExperienceEntities": [
{
"historyStartDate": "10.10.2015",
"historyEndDate": "10.10.2016",
"isWorking": false
}
]
我收到一个错误:
嵌套异常是 com.fasterxml.jackson.databind.JsonMappingException: Invalid format: "10.10.2015" is malformed at ".10.2015" (通过引用链: com.test.service.dto.EmployeeDTO["historyExperienceEntities"]->java .util.HashSet[0]->com.test.domain.HistoryExperienceEntity["historyStartDate"])
我尝试@JsonFormat
在 DTO 中的集合上方添加相同的注释,但没有奏效。