Java 8 LocalDate Jackson 格式 [英] Java 8 LocalDate Jackson format
问题描述
对于 java.util.Date 当我这样做时
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy")
private Date dateOfBirth;
然后在 JSON 请求中发送
then in JSON request when I send
{ {"dateOfBirth":"01/01/2000"} }
它有效.
我应该如何为 Java 8 的 LocalDate 字段执行此操作??
How should I do this for Java 8's LocalDate field??
我尝试过
@JsonDeserialize(using = LocalDateDeserializer.class)
@JsonSerialize(using = LocalDateSerializer.class)
private LocalDate dateOfBirth;
没用.
谁能告诉我正确的方法是什么..
Can someone please let me know what's the right way to do this..
以下是依赖项
<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>
推荐答案
我从来没有能够使用注释来简单地工作.为了让它工作,我创建了一个 ContextResolver
用于 ObjectMapper
,然后我添加了 JSR310Module
(更新:现在是 JavaTimeModule
),还有一个警告,那就是需要将 write-date-as-timestamp 设置为 false.在JSR310 模块的文档中查看更多信息.这是我使用的示例.
I was never able to get this to work simple using annotations. To get it to work, I created a ContextResolver
for ObjectMapper
, then I added the JSR310Module
(update: now it is JavaTimeModule
instead), 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>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.4.0</version>
</dependency>
注意:我遇到的一个问题是 jackson-annotation
版本被另一个依赖项引入,使用了 2.3.2 版本,它取消了jsr310
所需的 2.4.发生的事情是我得到了 ObjectIdResolver
的 NoClassDefFound,这是一个 2.4 类.所以我只需要排列包含的依赖版本
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 the jsr310
. What happened was I got a NoClassDefFound for ObjectIdResolver
, which is a 2.4 class. So I just needed to line up the included dependency versions
上下文解析器
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();
// Now you should use JavaTimeModule instead
MAPPER.registerModule(new JSR310Module());
MAPPER.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
}
@Override
public ObjectMapper getContext(Class<?> type) {
return MAPPER;
}
}
资源类
@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;
}
}
测试
curl -v http://localhost:8080/api/person
结果: {"birthDate":"2015-03-01"}
curl -v -POST -H "Content-Type:application/json" -d "{"birthDate":"2015-03-01"}" http://localhost:8080/api/person
结果: 2015-03-01
<小时>
另请参阅此处了解 JAXB 解决方案.
See also here for JAXB solution.
JSR310Module
从 Jackson 2.7 版开始被弃用.相反,您应该注册模块 JavaTimeModule
.它仍然是相同的依赖项.
The JSR310Module
is deprecated as of version 2.7 of Jackson. Instead, you should register the module JavaTimeModule
. It is still the same dependency.
这篇关于Java 8 LocalDate Jackson 格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!