Jaxb + T-shirt. Skipping empty fields in generated json

I have a code:

   return Response.status(Status.BAD_REQUEST).entity(errors).build();

Where: Responsecomes from this package: javax.ws.rs.core(jersey 1.9.1);

Where errorsis the instance:

@XmlRootElement
public class UserInfoValidationErrors {

    @XmlElement String username;
    @XmlElement String email;
...

Then I get the JSON result as follows: {"username":null,"email":"Email is not valid"}

If there is a way to escape nullthere?

+4
source share
2 answers

If you have a Jersey configured to use Jackson to serialize JSON, you can annotate your model classes with

@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)

If you want to configure Jersey to use Jackson, you can update your web.xml as follows:

<servlet-name>Jersey</servlet-name>
    <servlet-class>
        com.sun.jersey.spi.container.servlet.ServletContainer
    </servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.your.package;org.codehaus.jackson.jaxrs</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
+6
source

@XmlElement(nillable = true). , XML, , JSON.

+4

All Articles