3 Replies Latest reply on Nov 22, 2008 11:53 AM by lowecg2004

    Could not find MessageBodyWriter for response object of type

    lowecg2004

      Hello all,


      I'm trying to get a rest easy service to serve JSON and I'm having a problem with returning a simple POJO.  I get the following exception:


      01:56:12,040 ERROR [STDERR] java.lang.RuntimeException: Could not find MessageBodyWriter for response object of type: class [Lcom.site.session.rest.Book; of media type: application/json
      01:56:12,040 ERROR [STDERR]      at org.resteasy.ResourceMethod.writeResponse(ResourceMethod.java:218)
      01:56:12,040 ERROR [STDERR]      at org.resteasy.ResourceMethod.invoke(ResourceMethod.java:198)
      01:56:12,040 ERROR [STDERR]      at org.resteasy.ResourceMethod.invoke(ResourceMethod.java:134)
      01:56:12,040 ERROR [STDERR]      at org.resteasy.Dispatcher.invoke(Dispatcher.java:161)
      01:56:12,040 ERROR [STDERR]      at org.jboss.seam.resteasy.ResteasyResourceAdapter$1.process(ResteasyResourceAdapter.java:127)
      01:56:12,040 ERROR [STDERR]      at org.jboss.seam.servlet.ContextualHttpServletRequest.run(ContextualHttpServletRequest.java:53)
      01:56:12,041 ERROR [STDERR]      at org.jboss.seam.resteasy.ResteasyResourceAdapter.getResource(ResteasyResourceAdapter.java:71)
      01:56:12,041 ERROR [STDERR]      at org.jboss.seam.servlet.SeamResourceServlet.service(SeamResourceServlet.java:80)
      01:56:12,041 ERROR [STDERR]      at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
      01:56:12,041 ERROR [STDERR]      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
      01:56:12,041 ERROR [STDERR]      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      01:56:12,041 ERROR [STDERR]      at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
      01:56:12,041 ERROR [STDERR]      at org.jboss.seam.web.IdentityFilter.doFilter(IdentityFilter.java:38)
      01:56:12,041 ERROR [STDERR]      at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
      01:56:12,041 ERROR [STDERR]      at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:90)
      01:56:12,041 ERROR [STDERR]      at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
      01:56:12,041 ERROR [STDERR]      at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)
      01:56:12,041 ERROR [STDERR]      at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
      01:56:12,041 ERROR [STDERR]      at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
      01:56:12,041 ERROR [STDERR]      at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
      01:56:12,041 ERROR [STDERR]      at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:73)
      01:56:12,041 ERROR [STDERR]      at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:177)
      01:56:12,041 ERROR [STDERR]      at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:267)
      01:56:12,041 ERROR [STDERR]      at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:380)
      01:56:12,041 ERROR [STDERR]      at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:507)
      01:56:12,041 ERROR [STDERR]      at org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:56)
      01:56:12,041 ERROR [STDERR]      at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
      01:56:12,041 ERROR [STDERR]      at org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:58)
      01:56:12,041 ERROR [STDERR]      at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
      01:56:12,041 ERROR [STDERR]      at org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)
      01:56:12,041 ERROR [STDERR]      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
      01:56:12,041 ERROR [STDERR]      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      01:56:12,041 ERROR [STDERR]      at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
      01:56:12,041 ERROR [STDERR]      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
      01:56:12,042 ERROR [STDERR]      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      01:56:12,042 ERROR [STDERR]      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
      01:56:12,042 ERROR [STDERR]      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
      01:56:12,042 ERROR [STDERR]      at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:182)
      01:56:12,042 ERROR [STDERR]      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:432)
      01:56:12,042 ERROR [STDERR]      at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
      01:56:12,042 ERROR [STDERR]      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
      01:56:12,042 ERROR [STDERR]      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
      01:56:12,042 ERROR [STDERR]      at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
      01:56:12,042 ERROR [STDERR]      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
      01:56:12,042 ERROR [STDERR]      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
      01:56:12,042 ERROR [STDERR]      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
      01:56:12,042 ERROR [STDERR]      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
      01:56:12,042 ERROR [STDERR]      at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
      01:56:12,042 ERROR [STDERR]      at java.lang.Thread.run(Thread.java:619)



      Here is my test code:


      @Path("/testService")
      @Name("testServiceRest")
      @Scope(ScopeType.STATELESS)
      @ConsumeMime({"application/json"})
      @ProduceMime({"application/json"}) 
      public class TestServiceRest {
      
           @GET
           @Path("/getBooks")
           public Book[] getBooks() {
                final Book[] result = new Book[1];
                
                result[0] = new Book("Author", "ISBN", "Title");
                
                return result;
           }
      }
      
      ...
      
      import javax.xml.bind.annotation.XmlAttribute;
      import javax.xml.bind.annotation.XmlElement;
      import javax.xml.bind.annotation.XmlRootElement;
      
      @XmlRootElement(name = "book")
      public class Book
      {
         private String author;
         private String ISBN;
         private String title;
      
         public Book()
         {
         }
      
         public Book(String author, String ISBN, String title)
         {
            this.author = author;
            this.ISBN = ISBN;
            this.title = title;
         }
      
         @XmlElement
         public String getAuthor()
         {
            return author;
         }
      
         public void setAuthor(String author)
         {
            this.author = author;
         }
      
         @XmlElement
         public String getISBN()
         {
            return ISBN;
         }
      
         public void setISBN(String ISBN)
         {
            this.ISBN = ISBN;
         }
      
         @XmlAttribute
         public String getTitle()
         {
            return title;
         }
      
         public void setTitle(String title)
         {
            this.title = title;
         }
      }
      



      As I understood it, I thought Seam would marshal JSON out of the box. I've left the resteasy:application-config with all the defaults.


      Am I missing something or have I completely got this wrong?! I'd be grateful for a couple of pointers, I've search high and low for an answer to this and I've now reached a dead end.


      Cheers,


      Chris.



      BTW - I'm using Seam 2.1.0.GA.

        • 1. Re: Could not find MessageBodyWriter for response object of type
          lowecg2004

          Hello,


          Could any one tell me if they've got a Resteasy WS returning JSON?  I've double checked my settings as per the reference manual and I'm still unable to get my web service working.


          Cheers,


          Chris.

          • 2. Re: Could not find MessageBodyWriter for response object of type
            lowecg2004

            Still no joy.  After some more trawling I find this Jira case which looks like it might be the cause of my problems:


            RESTEASY-158


            (Which is currently the only unresolved issue for the resteasy beta 9 release.)


            The version of resteasy with Seam 2.1.0.GA is quite old, so I upgraded to the latest beta 8 as described here:


            JBSEAM-3449


            I also upgraded to Seam 2.1.1.CR1.


            In the meantime I just want to see some JSON being served, so I modified the test Seam component to remove the array:


            @Path("/testService")
            @Name("testServiceRest")
            @Scope(ScopeType.STATELESS)
            @Consumes({"application/json"})
            @Produces({"application/json"}) 
            public class TestServiceRest {
            
                 @GET
                 @Path("/getBooks")
                 public Book getBooks() {
                      final Book result = new Book("Author", "ISBN", "Title");
                      
                      return result;
                 }
            }




            Now I get this message:


            Caused by: java.lang.NoClassDefFoundError: javax/xml/namespace/QName
                 at com.sun.xml.bind.v2.model.impl.RuntimeBuiltinLeafInfoImpl.<clinit>(RuntimeBuiltinLeafInfoImpl.java:150)
                 at com.sun.xml.bind.v2.model.impl.RuntimeTypeInfoSetImpl.<init>(RuntimeTypeInfoSetImpl.java:25)
                 at com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.createTypeInfoSet(RuntimeModelBuilder.java:89)
                 at com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.createTypeInfoSet(RuntimeModelBuilder.java:42)
                 at com.sun.xml.bind.v2.model.impl.ModelBuilder.<init>(ModelBuilder.java:114)
                 at com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.<init>(RuntimeModelBuilder.java:49)
                 at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:389)
                 at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:253)
                 at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:84)
                 at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:66)
                 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                 at java.lang.reflect.Method.invoke(Method.java:597)
                 at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:211)
                 at javax.xml.bind.ContextFinder.find(ContextFinder.java:372)
                 at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:574)
                 at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:522)
                 at org.jboss.resteasy.plugins.providers.jaxb.json.JettisonMappedContext.<init>(JettisonMappedContext.java:71)
                 at org.jboss.resteasy.plugins.providers.jaxb.json.JettisonMappedContext.<init>(JettisonMappedContext.java:32)
                 at org.jboss.resteasy.plugins.providers.jaxb.json.JettisonJAXBContextFactory.find(JettisonJAXBContextFactory.java:67)
                 at org.jboss.resteasy.plugins.providers.jaxb.json.JettisonJAXBContextFactory.findJAXBContext(JettisonJAXBContextFactory.java:41)
                 at org.jboss.resteasy.plugins.providers.jaxb.json.JsonXmlRootElementProvider.findJAXBContext(JsonXmlRootElementProvider.java:33)
                 at org.jboss.resteasy.plugins.providers.jaxb.AbstractJAXBProvider.getMarshaller(AbstractJAXBProvider.java:144)
                 at org.jboss.resteasy.plugins.providers.jaxb.AbstractJAXBProvider.writeTo(AbstractJAXBProvider.java:120)
                 at org.jboss.resteasy.core.SynchronousDispatcher.writeJaxrsResponse(SynchronousDispatcher.java:432)
                 at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:357)
                 at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:182)
                 at org.jboss.resteasy.core.AsynchronousDispatcher.invoke(AsynchronousDispatcher.java:238)
                 at org.jboss.seam.resteasy.ResteasyResourceAdapter$1.process(ResteasyResourceAdapter.java:127)
                 at org.jboss.seam.servlet.ContextualHttpServletRequest.run(ContextualHttpServletRequest.java:53)
                 at org.jboss.seam.resteasy.ResteasyResourceAdapter.getResource(ResteasyResourceAdapter.java:71)
                 at org.jboss.seam.servlet.SeamResourceServlet.service(SeamResourceServlet.java:80)
                 at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
                 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
                 ... 39 more
            Caused by: java.lang.ClassNotFoundException: Unexpected error during load of: javax.xml.namespace.QName, msg=loader constraint violation: loader (instance of org/jboss/mx/loading/UnifiedClassLoader3) previously initiated loading for a different type with name "javax/xml/namespace/QName"
                 at org.jboss.mx.loading.RepositoryClassLoader.loadClassImpl(RepositoryClassLoader.java:560)
                 at org.jboss.mx.loading.RepositoryClassLoader.loadClass(RepositoryClassLoader.java:415)
                 at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
                 at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
                 ... 74 more
            Caused by: java.lang.LinkageError: loader constraint violation: loader (instance of org/jboss/mx/loading/UnifiedClassLoader3) previously initiated loading for a different type with name "javax/xml/namespace/QName"
                 at java.lang.ClassLoader.defineClass1(Native Method)
                 at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
                 at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
                 at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
                 at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
                 at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
                 at java.security.AccessController.doPrivileged(Native Method)
                 at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
                 at org.jboss.mx.loading.RepositoryClassLoader.findClassLocally(RepositoryClassLoader.java:690)
                 at org.jboss.mx.loading.RepositoryClassLoader.findClass(RepositoryClassLoader.java:670)
                 at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
                 at org.jboss.mx.loading.RepositoryClassLoader.loadClassLocally(RepositoryClassLoader.java:200)
                 at org.jboss.mx.loading.ClassLoadingTask$ThreadTask.run(ClassLoadingTask.java:131)
                 at org.jboss.mx.loading.LoadMgr3.nextTask(LoadMgr3.java:399)
                 at org.jboss.mx.loading.RepositoryClassLoader.loadClassImpl(RepositoryClassLoader.java:527)
                 ... 77 more




            I rolled back to the libs shipped with 2.1.0.GA and I got a similar error.


            Any ideas?

            • 3. Re: Could not find MessageBodyWriter for response object of type
              lowecg2004

              Obviously this was a class loading error and it was a case of catching a jar that had the offending QName class definition.  I went for the highly sophisticated approach and used grep on my entire project and JBoss server hierarchies ;) Turns out that I had a jaxb-impl.jar lurking in the deployed version of the app.  I'd long since removed it from my project's WEB-INF/lib but the jar remained in the exploded deploy folder.


              TIP: In Eclipse, run a Clean... on the JBoss server every once in a while ;)


              My server is now returning JSON.  I still can't return arrays or collections, but I noticed that RESTEASY-158 has now been marked as resolved/closed.  I'm hopeful of a Resteasy b9 release soon...