2

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?

2 Answers2

2

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.

bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • Thanks for yours answer. Actually I was searching for a way to hand out null values(you guessed correctly in #1) and changed the datatype of "id" from int to Integer, but forgot the setter. Next time I'll let the IDE refactor it. Thanks once more for your detailed helpful answer (I'd +1ed it, if I had enough reputation)! – gross.jonas May 07 '12 at 07:00
0

There's nothing special about "id". From the @XmlRootElement on your class, I assume you're using JAXB for marshalling (the default with Jersey). In a default configuration, JAXB requires a getter to marshal a field to XML/JSON and a setter to unmarshal a field from XML/JSON. In this case, I'd guess you're lacking a public Integer getId() method. You can easily change this behavior by adding @XmlElement to your id field.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199