如何反序列化Java 8时间API类?
这就是我所做的:
包含依赖项:compile("com.fasterxml.jackson.datatype:jackson-datatype-jsr310")
请求将序列反序列化为:
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.joda.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
public class TestRequest {
@JsonSerialize(using = LocalDateSerializer.class)
@JsonDeserialize(using = LocalDateDeserializer.class)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "YYYY-MM-dd")
LocalDate localDate;
}
Spring Controller:
public ResponseEntity test(@RequestBody TestRequest testRequest) {
return ResponseEntity.ok(testRequest);
}
JSON请求正文:
{ "localDate": "2017-11-01" }
例外:
com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of TestRequest, problem: argument type mismatch