Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RESTEasy and ContextResolver<ObjectMapper> for Jackson

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.

like image 544
Ging3r Avatar asked Sep 13 '13 09:09

Ging3r


1 Answers

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);
    }
}
like image 63
gregwhitaker Avatar answered Oct 06 '22 00:10

gregwhitaker