对于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>
如果你的请求包含一个这样的对象:
{
"year": 1900,
"month": 1,
"day": 20
}
然后你可以使用:
data class DateObject(
val day: Int,
val month: Int,
val year: Int
)
class LocalDateConverter : StdConverter<DateObject, LocalDate>() {
override fun convert(value: DateObject): LocalDate {
return value.run { LocalDate.of(year, month, day) }
}
}
以上字段:
@JsonDeserialize(converter = LocalDateConverter::class)
val dateOfBirth: LocalDate
代码是在Kotlin中,但这当然也适用于Java。
到2020年和Jackson 2.10.1,不需要任何特殊的代码,只需要告诉Jackson你想要什么:
ObjectMapper objectMapper = new ObjectMapper();
// Register module that knows how to serialize java.time objects
// Provided by jackson-datatype-jsr310
objectMapper.registerModule(new JavaTimeModule());
// Ask Jackson to serialize dates as String (ISO-8601 by default)
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
这已经在这个答案中提到了,我添加了一个单元测试来验证功能:
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import lombok.Data;
import org.junit.jupiter.api.Test;
import java.time.LocalDate;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class LocalDateSerializationTest {
@Data
static class TestBean {
// Accept default ISO-8601 format
LocalDate birthDate;
// Use custom format
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy")
LocalDate birthDateWithCustomFormat;
}
@Test
void serializeDeserializeTest() throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
// Register module that knows how to serialize java.time objects
objectMapper.registerModule(new JavaTimeModule());
// Ask Jackson to serialize dates as String (ISO-8601 by default)
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
// The JSON string after serialization
String json = "{\"birthDate\":\"2000-01-02\",\"birthDateWithCustomFormat\":\"03/02/2001\"}";
// The object after deserialization
TestBean object = new TestBean();
object.setBirthDate(LocalDate.of(2000, 1, 2));
object.setBirthDateWithCustomFormat(LocalDate.of(2001, 2, 3));
// Assert serialization
assertEquals(json, objectMapper.writeValueAsString(object));
// Assert deserialization
assertEquals(object, objectMapper.readValue(json, TestBean.class));
}
}
TestBean使用Lombok为bean生成样板。
@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));
}
}