Java 8 LocalDate Jackson格式 [英] Java 8 LocalDate Jackson format
问题描述
对于 java.util.Date 当我这样做时
@JsonFormat(shape = JsonFormat。 Shape.STRING,pattern =dd / MM / yyyy)
,然后我添加了
private Date dateOfBirth;我发送{{dateOfBirth:01/01/2000}}
它有效。
我应该如何为 Java 8的LocalDate 字段做这个?
我试过
@JsonDeserialize(using = LocalDateDeserializer.class)
@JsonSerialize(using = LocalDateSerializer.class)
private LocalDate dateOfBirth;
它不起作用。
有人可以让我知道这样做的正确方法..
以下是依赖项
<依赖性>
< 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>
< dependency>
解决方案我从来没有能够简单地使用它注释。为了使它工作,我创建了一个 <$
ObjectMapper
的c $ c> ContextResolverJSR310Module
,还有一个警告,需要将write-date-as-timestamp设置为false。有关详情,请参阅 JSR310模块的文档。以下是我使用的示例。
依赖性
<依赖性>
< groupId> com.fasterxml.jackson.datatype< / groupId>
< artifactId> jackson-datatype-jsr310< / artifactId>
< version> 2.4.0< / version>
< / dependency>
注意:我遇到的一个问题是
jackson-annotation
由另一个依赖项引入的版本,使用版本2.3.2,它取消了jsr310
。发生的事情是我为ObjectIdResolver
获得了NoClassDefFound,这是一个2.4类。所以我只需要排列包含的依赖版本
ContextResolver
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JSR310Module;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
@Provider
公共类ObjectMapperContextResolver实现ContextResolver< ObjectMapper> {
私人最终的ObjectMapper MAPPER;
public ObjectMapperContextResolver(){
MAPPER = new ObjectMapper();
MAPPER.registerModule(new JSR310Module());
MAPPER.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
}
@Override
public ObjectMapper getContext(Class<?> type){
return MAPPER;
}
}
资源等级
@Path(person)
公共类LocalDateResource {
@GET
@Produces(MediaType.APPLICATION_JSON )
public Response getPerson(){
Person person = new Person();
person.birthDate = LocalDate.now();
返回Response.ok(person).build();
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response createPerson(Person person){
return Response.ok(
DateTimeFormatter.ISO_DATE.format(person.birthDate))。build();
}
public static class Person {
public LocalDate birthDate;
}
}
测试
curl -v http:// localhost:8080 / api / person
结果:{birthDate:2015-03-01}
curl -v -POST -HContent-Type:application / json-d{\birthDate \:\2015-03-01 \}http:// localhost: 8080 / api / person
结果:2015-03-01
另见 此处 适用于JAXB解决方案。
更新
从Jackson 2.7版本开始,不推荐使用
JSR310Module
。相反,您应该注册模块JavaTimeModule
。它仍然是相同的依赖。For java.util.Date when I do
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy") private Date dateOfBirth;
then in JSON request when I send
{ {"dateOfBirth":"01/01/2000"} }
it works.
How should I do this for Java 8's LocalDate field??
I tried having
@JsonDeserialize(using = LocalDateDeserializer.class) @JsonSerialize(using = LocalDateSerializer.class) private LocalDate dateOfBirth;
It didn't work.
Can someone please let me know what's the right way to do this..
Below are dependencies
<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> <dependency>
解决方案I was never able to get this to work simple using annotations. To get it to work, I created a
ContextResolver
forObjectMapper
, then I added theJSR310Module
, along with one more caveat, which was the need to set write-date-as-timestamp to false. See more at the documentation for the JSR310 module. Here's an example of what I used.Dependency
<dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-jsr310</artifactId> <version>2.4.0</version> </dependency>
Note: One problem I faced with this is that the
jackson-annotation
version pulled in by another dependency, used version 2.3.2, which cancelled out the 2.4 required by thejsr310
. What happened was I got a NoClassDefFound forObjectIdResolver
, which is a 2.4 class. So I just needed to line up the included dependency versionsContextResolver
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.datatype.jsr310.JSR310Module; import javax.ws.rs.ext.ContextResolver; import javax.ws.rs.ext.Provider; @Provider public class ObjectMapperContextResolver implements ContextResolver<ObjectMapper> { private final ObjectMapper MAPPER; public ObjectMapperContextResolver() { MAPPER = new ObjectMapper(); MAPPER.registerModule(new JSR310Module()); MAPPER.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); } @Override public ObjectMapper getContext(Class<?> type) { return MAPPER; } }
Resource class
@Path("person") public class LocalDateResource { @GET @Produces(MediaType.APPLICATION_JSON) public Response getPerson() { Person person = new Person(); person.birthDate = LocalDate.now(); return Response.ok(person).build(); } @POST @Consumes(MediaType.APPLICATION_JSON) public Response createPerson(Person person) { return Response.ok( DateTimeFormatter.ISO_DATE.format(person.birthDate)).build(); } public static class Person { public LocalDate birthDate; } }
Test
curl -v http://localhost:8080/api/person
Result:{"birthDate":"2015-03-01"}
curl -v -POST -H "Content-Type:application/json" -d "{\"birthDate\":\"2015-03-01\"}" http://localhost:8080/api/person
Result:2015-03-01
See also here for JAXB solution.
UPDATE
The
JSR310Module
is deprecated as of version 2.7 of Jackson. Instead, you should register the moduleJavaTimeModule
. It is still the same dependency.这篇关于Java 8 LocalDate Jackson格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!