RESTeasy, JSON and empty fields

When RESTeasy marshals POJOs to XML, it skips null values ​​by default.

However, null properties are included when marshaling to JSON. Is there a way to get the JSON output to match the XML output?

Also I tried @XmlElement (required = false, nillable = true)) and it didn't work. I've only used RESTeasy with annotations.

+3


source to share


1 answer


Using Jackson 2. Install the following provider:

package com.recruitinghop.swagger;

import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.Provider;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import com.fasterxml.jackson.module.scala.DefaultScalaModule;

@Provider
@Produces(MediaType.APPLICATION_JSON)
public class JacksonJsonProvider extends JacksonJaxbJsonProvider {

    public JacksonJsonProvider() {
          ObjectMapper mapper = new ObjectMapper();
          mapper.registerModule(new DefaultScalaModule());
          mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
          mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);
          mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
          mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
          super.setMapper(mapper);
    }

}

      



The Scala module is optional.

+5


source