我的 RESTEasy JSON 响应的自定义有问题。
在web.xml
我使用自动扫描:
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param>
这是我的自定义类ObjectMapper
,(我设置了非空字段和新的人类可读日期):
@Provider
@Produces(MediaType.APPLICATION_JSON)
public class JacksonConfig implements ContextResolver<ObjectMapper> {
private Logger log = Logger.getLogger(PropertiesConfig.LOG_CATEGORY);
private ObjectMapper objectMapper;
public JacksonConfig() throws Exception {
objectMapper = new ObjectMapper();
objectMapper.getSerializationConfig().setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
objectMapper.setDateFormat(new SimpleDateFormat("dd.MM.yyyy"));
objectMapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
}
@Override
public ObjectMapper getContext(Class<?> arg0) {
return objectMapper;
}
}
这是我的小服务程序:
@Path("/search")
public class Search extends ArtesAPI {
@GET
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
public Response search(@Context HttpServletRequest request){
RequestManager reqManager = new RequestManager(request);
MyResponse response = reqManager.doSearchRequest();
return Response.status(200).entity(response).build();
}
}
当我部署到服务器时,RESTEasy 会打印以下日志:
10:43:44,320 INFO [TomcatDeployment] deploy, ctxPath=/MyServer
10:43:44,544 INFO [ConfigurationBootstrap] Adding scanned @Provider: myserver.servlets.JacksonConfig
10:43:44,545 INFO [ConfigurationBootstrap] Adding scanned resource: myserver.servlets.Search
但是当我调用搜索 API 时,我收到了这个响应:(这里是响应的一小部分)
{
"entry": [
{
"name": "abbigliamento",
"description": null,
"lastUpdate": 1375448941000,
"subCategory": null
},
{
"name": "car",
"description": null,
"lastUpdate": null,
"subCategory": null
}
}
我的服务器响应给了我空字段,lastUpdate
以毫秒为单位。
我哪里出错了?
提前致谢。