2 Replies Latest reply on Apr 15, 2013 7:43 PM by Keith Babo

    rest-binding quickstart - need help with returning response in JSON

    Ross Stockman Newbie

      I am using the rest-binding quickstart to demo a simple rest service that will return a json response on a GET request but I am not getting the response that I am expecting. First I modified the WarehouseResource class so that the @GET method would return a json result:

       

      @GET

      @Path("{itemId}")

      @Produces({"application/json"})

      public Item getItem(@PathParam("itemId") Integer itemId);

       

      I also added an additional field to the Item class that for the purposes of this demo will always return null:

       

      @XmlAccessorType(XmlAccessType.FIELD)

      @XmlRootElement(name = "item")

      public class Item {

         @XmlElement(name = "itemId", required = true)

          private Integer _itemId;

          @XmlElement(name = "changedName", required = false)

          private String _name;

          @XmlElement(name = "count", required = false)

          private String _count;

      ...

      }

       

      Then I deploy and call the PUT method: http://localhost:8080/rest-binding/warehouse/1/testing

      Then I call the GET method http://localhost:8080/rest-binding/warehouse/1

       

      I get the following response.

       

          {

             "count": null,

             "itemId": 1,

             "name": "testing"

          }

       

      The two problems I need to solve (#2 being highest priority):

       

      1. the jaxb annotations seem to be ignored when marshalling in json because the "name" field in the response should have been called "changedName" as indicated in the @XmlElement. Is there a way to use jaxb annotations to map the appropriate field names in the json response or is there a way to add a custom marshaller to format the json response?

       

      2. the "count" field is included in the response even though it has a null value. Is there a way to ignore or omit null fields so they are not included in the json response?

       

      FYI - if I set @Produces back to "application/xml" then the field names are being set as expected from the @XmlElement annotations and the null fields are not included in the response as expected.

        • 1. Re: rest-binding quickstart - need help with returning response in JSON
          Ross Stockman Newbie

          I found a solution. I am now getting the json response that is fully utilizying the jaxb annotations and is supressing null fields. I added the following dependency to the pom:

           

          <dependency>

                                        <groupId>org.jboss.resteasy</groupId>

                                        <artifactId>resteasy-jackson-provider</artifactId>

                                        <version>2.3.5.Final</version>

                                        <scope>provided</scope>

          </dependency>

           

          Then add the following @NoJackson annotation to the jaxb annotated classes.

           

           

          import org.jboss.resteasy.annotations.providers.NoJackson;

          ...

           

          @XmlAccessorType(XmlAccessType.FIELD)

          @XmlRootElement(name = "item")

          @NoJackson

          public class Item {

             @XmlElement(name = "itemId", required = true)

              private Integer _itemId;

              @XmlElement(name = "changedName", required = false)

              private String _name;

              @XmlElement(name = "count", required = false)

              private String _count;

          ...

          }

          • 2. Re: rest-binding quickstart - need help with returning response in JSON
            Keith Babo Master

            Thanks for posting your solution back to the forum.