1 Reply Latest reply on Jul 17, 2014 3:27 PM by Bradley Snyder

    resteasy with jaxb and json

    Alex Soto Newbie

      Hi, I am developing an application which I want to be "100% fully compliant" across different Java EE vendors. Now it is a simple RESTful web application. The application works perfectly in Apache TomEE and Glassfish but not in Wildfly 8.1.0. Concretely it fails when try to unmarshal a rest call. Let me show an example (I have created one to make easy to follow).

       

      I have next POJO:

       

      @XmlRootElement
      public class BookInfo {
          private String title;
          private String author;
      
          public void setAuthor(String author) {
              this.author = author;
          }
      
          public String getAuthor() {
              return author;
          }
          public void setTitle(String title) {
              this.title = title;
          }
      
          public String getTitle() {
              return title;
          }
      }
      

       

      And the service looks like:

       

      @Singleton
      @Path("bookinfo")
      public class BookInfoService {
          @GET
          @Path("{isbn}")
          @Produces(MediaType.APPLICATION_JSON)
          @Consumes(MediaType.APPLICATION_JSON)
          public BookInfo findBookByISBN(@PathParam("isbn") String isbn) {
      
              BookInfo bookInfo = new BookInfo();
              bookInfo.setAuthor("XX");
              bookInfo.setTitle("YY");
             
              return bookInfo;
          }
      }
      

       

      And I am using Arquillian and the test method is:

       

       Client client = ClientBuilder.newClient();
       BookInfo bookInfo = client.target(URI.create(new URL(base, "rest/").toExternalForm())).path("bookinfo/1111").request().get(BookInfo.class);
      

       

      I am using Arquillian 1.1.5, Wildfly 8.1.0 and Jettison 3.0.8.

       

      And the exception that is thrown in line 2 of test is:

       

      javax.ws.rs.client.ResponseProcessingException: javax.ws.rs.ProcessingException: org.jboss.resteasy.plugins.providers.jaxb.JAXBUnmarshalException: javax.xml.bind.UnmarshalException
       - with linked exception:
      [com.sun.istack.SAXParseException2; columnNumber: 0; unexpected element (uri:"", local:"title"). Expected elements are <{}bookInfo>]
        at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.extractResult(ClientInvocation.java:140)
        at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:444)
        at org.jboss.resteasy.client.jaxrs.internal.ClientInvocationBuilder.get(ClientInvocationBuilder.java:165)
      

       

      I am not pretty sure why it works in all servers but not in Wildfly. I have googled a bit but I have not found any solution to this problem

       

      If in test I do a get(String.class) I receive a correct json document so it seems that server side works perfectly.

       

      Thank you so much for your time,

      Alex.

        • 1. Re: resteasy with jaxb and json
          Bradley Snyder Newbie

          I'm not sure the JAX-RS 2.0 Client API is required to automatically unmarshal JSON into an object. That exception to me looks like it's trying to unmarshal the body as JAXB (so in your case, it's looking for <bookInfo><title> ...). You can try changing request() to request(MediaType.APPLICATION_JSON_TYPE) to further indicate what you expect, but I think you're going to have to implement a JAX-RS MessageBodyReader that handles the JSON.