对于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>
因为LocalDateSerializer默认将其转换为“[年,月,日]”(json数组)而不是“年-月-日”(json字符串),并且因为我不想需要任何特殊的ObjectMapper设置(如果禁用SerializationFeature,您可以使LocalDateSerializer生成字符串)。WRITE_DATES_AS_TIMESTAMPS,但这需要额外的设置到您的ObjectMapper),我使用以下:
进口:
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
代码:
// generates "yyyy-MM-dd" output
@JsonSerialize(using = ToStringSerializer.class)
// handles "yyyy-MM-dd" input just fine (note: "yyyy-M-d" format will not work)
@JsonDeserialize(using = LocalDateDeserializer.class)
private LocalDate localDate;
现在我可以使用new ObjectMapper()来读写我的对象,而不需要任何特殊的设置。
@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默认将其转换为“[年,月,日]”(json数组)而不是“年-月-日”(json字符串),并且因为我不想需要任何特殊的ObjectMapper设置(如果禁用SerializationFeature,您可以使LocalDateSerializer生成字符串)。WRITE_DATES_AS_TIMESTAMPS,但这需要额外的设置到您的ObjectMapper),我使用以下:
进口:
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
代码:
// generates "yyyy-MM-dd" output
@JsonSerialize(using = ToStringSerializer.class)
// handles "yyyy-MM-dd" input just fine (note: "yyyy-M-d" format will not work)
@JsonDeserialize(using = LocalDateDeserializer.class)
private LocalDate localDate;
现在我可以使用new ObjectMapper()来读写我的对象,而不需要任何特殊的设置。