对于java.util.Date
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy")
private Date dateOfBirth;
然后在JSON请求中发送
{ {"dateOfBirth":"01/01/2000"} }
它的工作原理。
我应该如何为Java 8的LocalDate字段这样做??
我试着
@JsonDeserialize(using = LocalDateDeserializer.class)
@JsonSerialize(using = LocalDateSerializer.class)
private LocalDate dateOfBirth;
但是没有成功。
谁能告诉我正确的方法是什么?
下面是依赖关系
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>jaxrs-api</artifactId>
<version>3.0.9.Final</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>com.wordnik</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.3.10</version>
</dependency>
https://stackoverflow.com/a/53251526/1282532是序列化/反序列化属性的最简单方法。对于这种方法,我有两个顾虑——在某种程度上违反了DRY原则,以及pojo和mapper之间的高耦合。
public class Trade {
@JsonFormat(pattern = "yyyyMMdd")
@JsonDeserialize(using = LocalDateDeserializer.class)
@JsonSerialize(using = LocalDateSerializer.class)
private LocalDate tradeDate;
@JsonFormat(pattern = "yyyyMMdd")
@JsonDeserialize(using = LocalDateDeserializer.class)
@JsonSerialize(using = LocalDateSerializer.class)
private LocalDate maturityDate;
@JsonFormat(pattern = "yyyyMMdd")
@JsonDeserialize(using = LocalDateDeserializer.class)
@JsonSerialize(using = LocalDateSerializer.class)
private LocalDate entryDate;
}
如果你有多个LocalDate字段的POJO,最好配置映射器而不是POJO。如果您使用的是ISO-8601值("2019-01-31"),可以简单到https://stackoverflow.com/a/35062824/1282532
如果你需要处理自定义格式,代码将是这样的:
ObjectMapper mapper = new ObjectMapper();
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyyMMdd")));
javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyyMMdd")));
mapper.registerModule(javaTimeModule);
逻辑只编写一次,它可以用于多个POJO
@JsonSerialize和@JsonDeserialize工作得很好。它们消除了导入额外jsr310模块的需要:
@JsonDeserialize(using = LocalDateDeserializer.class)
@JsonSerialize(using = LocalDateSerializer.class)
private LocalDate dateOfBirth;
反序列化器:
public class LocalDateDeserializer extends StdDeserializer<LocalDate> {
private static final long serialVersionUID = 1L;
protected LocalDateDeserializer() {
super(LocalDate.class);
}
@Override
public LocalDate deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
return LocalDate.parse(jp.readValueAs(String.class));
}
}
序列化器:
public class LocalDateSerializer extends StdSerializer<LocalDate> {
private static final long serialVersionUID = 1L;
public LocalDateSerializer(){
super(LocalDate.class);
}
@Override
public void serialize(LocalDate value, JsonGenerator gen, SerializerProvider sp) throws IOException, JsonProcessingException {
gen.writeString(value.format(DateTimeFormatter.ISO_LOCAL_DATE));
}
}
在配置类中定义LocalDateSerializer和LocalDateDeserializer类,并通过JavaTimeModule将它们注册到ObjectMapper,如下所示:
@Configuration
public class AppConfig
{
@Bean
public ObjectMapper objectMapper()
{
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(Include.NON_EMPTY);
//other mapper configs
// Customize de-serialization
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer());
javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer());
mapper.registerModule(javaTimeModule);
return mapper;
}
public class LocalDateSerializer extends JsonSerializer<LocalDate> {
@Override
public void serialize(LocalDate value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeString(value.format(Constant.DATE_TIME_FORMATTER));
}
}
public class LocalDateDeserializer extends JsonDeserializer<LocalDate> {
@Override
public LocalDate deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
return LocalDate.parse(p.getValueAsString(), Constant.DATE_TIME_FORMATTER);
}
}
}