Reputation: 55
When RESTeasy marshals a POJO into XML, it will skip null values by default.
However, when marshaling to JSON, null properties are included. Is there any way to force the JSON output to match the XML output?
Also i tried @XmlElement(required=false, nillable=true)) and it's not worked. I've been used only RESTeasy with Annotations.
Upvotes: 4
Views: 9303
Reputation: 49075
Use Jackson 2. Setup 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.
Upvotes: 5