gpt4 book ai didi

Java 8 LocalDate jackson 格式

转载 作者:IT老高 更新时间:2023-10-28 11:27:00 25 4
gpt4 key购买 nike

对于 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>

最佳答案

我从来没有能够使用注释让这个工作变得简单。为了让它工作,我创建了一个 ContextResolver对于 ObjectMapper,然后我添加了 JSR310Module(更新:现在它是 JavaTimeModule 而不是),以及另外一个需要注意的是,需要将 write-date-as-timestamp 设置为 false。在 the documentation for the JSR310 module 上查看更多信息.这是我使用的示例。

依赖

<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 类。所以我只需要排列包含的依赖版本

上下文解析器

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
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


另见 here用于 JAXB 解决方案。

更新

JSR310Module 自 Jackson 2.7 版起已弃用。相反,您应该注册模块 JavaTimeModule。还是一样的依赖。

关于Java 8 LocalDate jackson 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28802544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com