这是我的端点,我想在其中添加带有发布请求的“Firma”,但 JSON
无法隐式解析时间戳
。
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response addFirma(Firma firma){
firmaFacade.create(firma);
return Response.ok().build();
}
这些是“Firma”的变量
private int firm1;
private LocalDate firm2;
And this is the JSON-String I sent - LocalDate is NULL
{
"firm1":2,
"firm2":"2017-09-09"
}
但是每当我将 get 请求与测试数据一起使用时,它都会向我显示正确的结果:
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response findFirma() {
List<Firma> list = firmaFacade.findAll();
GenericEntity<List<Firma>> result = new GenericEntity<List<Firma>>(list) { };
return Response.ok().entity(result).build();
}
请帮助某人。
最佳答案
如果您使用的是 Jackson,请添加以下内容 dependency到您的项目:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson.version}</version>
</dependency>
然后创建一个 ContextResolver
对于 ObjectMapper
并注册JavaTimeModule
:
@Provider
public class ObjectMapperContextResolver implements ContextResolver<ObjectMapper> {
private final ObjectMapper mapper;
public ObjectMapperContextResolver() {
this.mapper = createObjectMapper();
}
@Override
public ObjectMapper getContext(Class<?> type) {
return mapper;
}
private ObjectMapper createObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
return mapper;
}
}
关于java - 使用 Post @Consumes(MediaType.ApplicationJson) 隐式解析 LocalDate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48172558/