Java 使用Post@Consumes隐式解析LocalDate(MediaType.ApplicationJson)
这是我的端点,我想在其中添加一个带有post请求的“Firma”,但是Java 使用Post@Consumes隐式解析LocalDate(MediaType.ApplicationJson),java,json,jax-rs,java-time,localdate,Java,Json,Jax Rs,Java Time,Localdate,这是我的端点,我想在其中添加一个带有post请求的“Firma”,但是JSON无法隐式解析时间戳 @POST @Consumes(MediaType.APPLICATION_JSON) public Response addFirma(Firma firma){ firmaFacade.create(firma); return Response.ok().build(); } 这些是“Firma”的变量 这是我发送的JSON字符串-LocalDate为空 但是,每当我对测试
JSON
无法隐式解析时间戳
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response addFirma(Firma firma){
firmaFacade.create(firma);
return Response.ok().build();
}
这些是“Firma”的变量
这是我发送的JSON字符串-LocalDate为空
但是,每当我对测试数据使用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();
}
@GET
@产生(MediaType.APPLICATION_JSON)
公共响应FindForma(){
List List=firmaFacade.findAll();
GenericEntity结果=新的GenericEntity(列表){};
返回Response.ok().entity(result.build();
}
请帮助他人。如果您正在使用Jackson,请将以下内容添加到您的项目中:
com.fasterxml.jackson.datatype
申请并注册:
@Provider
公共类ObjectMapperContextResolver实现ContextResolver{
私有最终对象映射器映射器;
公共对象MapPercontextResolver(){
this.mapper=createObjectMapper();
}
@凌驾
公共对象映射器getContext(类类型){
返回映射器;
}
私有对象映射器createObjectMapper(){
ObjectMapper mapper=新的ObjectMapper();
registerModule(新的JavaTimeModule());
禁用(SerializationFeature.WRITE_DATES_作为时间戳);
返回映射器;
}
}
谢谢你,凯西奥,但是在复制粘贴你给我的代码之后,我该如何使用映射器呢?该值仍然为空
{
"firm1":2,
"firm2":"2017-09-09"
}
@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();
}