javaspringjerseyjsr311

Jersey loses id field


I am using Jersey as JSR 311 implementation. The object i am trying to convert to JSON looks like this:

@XmlRootElement
public class deliveryTimes{
  private Integer id;
  private Short type;
  private Integer orderId;
  /* ... more fields and autogenerated Getters & Setters ... */
}

The JSON result is:

{"deliveryTimes":
[{"type":"1","orderId":"30449",/* ... other fields ... */ },
/* ... */
]}

In words: The field "id" is getting lost. In my other objects the id-fields have other names like orderId, customerId, ... and these fields don't get lost.

My pom.xml looks like this:

<!-- other stuff -->
<!--  JERSEY -->
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-server</artifactId>
    <version>1.11</version>
</dependency>
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>1.11</version>
</dependency>
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-grizzly2</artifactId>
    <version>1.11</version>
</dependency>
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-servlet</artifactId>
    <version>1.11</version>
</dependency>

<!-- Jersey + Spring -->
<dependency>
    <groupId>com.sun.jersey.contribs</groupId>
    <artifactId>jersey-spring</artifactId>
    <version>1.11</version>
    <exclusions>
        <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<!-- other stuff -->

There's no further configuration. I didn't find anything helpful on the jersey website or via google, so I ended up here with my first post ever.

Is there any config option I am missing? How do you JSONify id-fields?


Solution

  • There are a number of reasons why the id may not be marshalled.

    1 - It has a null value

    By default JAXB implementations will not marshal out null values. If you wish to marshal out a null value be sure to add the following annotation

    @XmlElement(nillable=true)
    

    For More Information See:

    2 - There is a field but not no accessor (get/set) methods

    By default JAXB only maps public fields and accessors. Anything not matching this criteria is considered unmapped. You can solve this issue by specifying XmlAccessorType(XmlAccessType.FIELD)

    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    public class deliveryTimes{
        ...
    }
    

    or by annotating the field

    @XmlRootElement
    public class deliveryTimes{
        @XmlElement
        private Integer id;
    }
    

    For More Information See:

    3 - There is a get method but no set method

    If there is a get method but no accompanying set method then your JAXB implementation will treat it as an unmapped property. To fix this you simply need to map the get method with @XmlElement.