7

我的 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以毫秒为单位。

我哪里出错了?

提前致谢。

4

2 回答 2

5

您可以通过扩展来自定义 Resteasy 中的 Jackson 序列化JacksonJsonProvider

@Provider
public class MyJacksonJsonProvider extends JacksonJsonProvider
{
    @Override
    public void writeTo(Object value, Class<?> type, Type genericType, Annotation[] annotations, 
            MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) 
            throws IOException 
    {
        ObjectMapper mapper = locateMapper(type, mediaType);

        mapper.getSerializationConfig().setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
        mapper.setDateFormat(new SimpleDateFormat("dd.MM.yyyy"));  
        mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);

        super.writeTo(value, type, genericType, annotations, mediaType, httpHeaders, entityStream);
    }
}
于 2013-09-13T20:15:41.280 回答
4

感谢 Greg Whitaker*,我找到了解决方案!

我不得不添加这个依赖

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-jaxrs</artifactId>
    <version>1.9.13</version>
</dependency>
  • 他的解决方案迫使我添加这种依赖。
于 2013-09-16T13:13:51.597 回答