8

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.

blacktide
  • 10,654
  • 8
  • 33
  • 53
Ging3r
  • 2,010
  • 2
  • 17
  • 26

2 Answers2

6

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);
    }
}
gregwhitaker
  • 13,124
  • 7
  • 69
  • 78
  • thanks to you I found the problem!...your suggest is correct, but my code too ^_^...I have forgot a dependency: org.codehaus.jackson.jackson-jaxrs – Ging3r Sep 16 '13 at 13:05
  • I think in your solution you should add the annotations as well `@Produces(MediaType.APPLICATION_JSON)` and/or `@Consumes(MediaType.APPLICATION_JSON)` because the class `JacksonJsonProvider` has too generic annotaions `@Provider @Consumes({"*/*"}) @Produces({"*/*"})` so it will be chosen as last solution if there is a more specific one already instatiated as `ResteasyJackson2Provider `. – felix at housecat Oct 05 '17 at 13:51
5

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.
Ging3r
  • 2,010
  • 2
  • 17
  • 26