RESTEasy and ContextResolver<ObjectMapper> for Jackson

谁都会走 提交于 2019-12-10 14:18:52

问题


I have a problem with the customization of my RESTEasy JSON response.

In web.xml I use autoscan:

<context-param>
    <param-name>resteasy.scan</param-name>
    <param-value>true</param-value>
</context-param>

Here is my customization class for ObjectMapper, (I've set not null fields and new human readable date):

@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;
    }  
}

Here is my servlet:

@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();
    }
}

When I deploy to the server, RESTEasy prints this log:

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

But when I call the search API, I receive this response: (here a little part of response)

{
"entry": [
    {
        "name": "abbigliamento",
        "description": null,
        "lastUpdate": 1375448941000,
        "subCategory": null
    },
    {
        "name": "car",
        "description": null,
        "lastUpdate": null,
        "subCategory": null
    }
}

My server response gives me null fields and lastUpdate in milliseconds.

Where have I gone wrong?

Thanks in advance.


回答1:


You can customize Jackson serialization in Resteasy by extending 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);
    }
}



回答2:


thanks to Greg Whitaker*, i found the solution!

I had to add this dependency

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-jaxrs</artifactId>
    <version>1.9.13</version>
</dependency>
  • his solution forced me to add this dependency.


来源:https://stackoverflow.com/questions/18782313/resteasy-and-contextresolverobjectmapper-for-jackson

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!