5 Replies Latest reply on Jan 1, 2010 3:55 PM by lvdberg

    Json Problem in Seam using Resteasy

      I am playing with jboss seam, resteasy to expose REST interface, I can get data in form of XML and text, but there is a problem when I'm trying to get the data in form of Json.
      I follow the document and example but still I have this problem.
      Any insight would be appreciated!

      List of Version that I use
      jboss-seam-2.1.2
      jboss-4.2.2.GA
      resteasy-jaxrs-1.2.1.GA

      I leave the web.xml and component.xml as default

      When I try to browse the URL:
      http://localhost:8080/cuichao/seam/resource/rest/customer/book
      it turns out HTTP Status 403 - Access to the requested resource has been denied

      And in the server.log,it seems it can't find the org.codehaus.jettison.mapped.MappedNamespaceConvention,
      but I have already included that in my project.

      Here is my code:

      package com.mydomain.cuichao.model;
      import javax.persistence.Entity;
      import javax.xml.bind.annotation.XmlRootElement;
      import javax.xml.bind.annotation.XmlElement;
      import javax.xml.bind.annotation.XmlAttribute;
      @XmlRootElement(name = "book")
      public class Book implements java.io.Serializable
      {
         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;
         }
      }

      package com.mydomain.cuichao.action;
      import java.io.StringWriter;
      import java.util.ArrayList;
      import java.util.Collection;
      import java.util.HashMap;
      import java.util.Iterator;
      import java.util.List;
      import javax.ws.rs.*;
      import org.jboss.resteasy.annotations.providers.jaxb.json.BadgerFish;
      import org.jboss.resteasy.annotations.providers.jaxb.json.Mapped;
      import com.mydomain.cuichao.model.Book;
      @Path("/customer")
      public class MyCustomerResource {
           private HashMap<String, Book> books = new HashMap<String, Book>();
           public MyCustomerResource(){
                books.put("596529260", new Book("Leonard Richardson", "596529260", "RESTful Web Services"));
                 books.put("333333333", new Book("Bill Burke", "596529260", "EJB 3.0"));
           }
           @GET
           @Path("/{customerId}")
           @Produces("text/plain")
           public String getCustomer(@PathParam("customerId") int id) {
                return "afd"+id;
           }
           @GET
           @Path("/book")
           @Mapped
           @Produces("application/json")   //Problem here
           public Book getOneBook(){
               Collection<Book> list = books.values();
               Iterator<Book> iter =  list.iterator();
               Book b = iter.next();
               System.out.println("*****BookListing getBooksMapped()******"+b.getTitle());
               return  b;
           }
           @GET
           @Path("/bookxml")                 //it works fine
           @Produces("application/xml")
           public Book getOneBookXML(){
               Collection<Book> list = books.values();
               Iterator<Book> iter =  list.iterator();
               Book b = iter.next();
               System.out.println("*****BookListing getBooksMapped()******"+b.getTitle());
               return  b;
           }
      }




        • 1. Re: Json Problem in Seam using Resteasy
          lvdberg

          Hi,


          I don't see a @Name on your rest-service. maybe that is the problem?


          Leo

          • 2. Re: Json Problem in Seam using Resteasy
            <blockquote>
            _Leo van den Berg wrote on Dec 29, 2009 20:11:_<br/>

            Hi,

            I don't see a @Name on your rest-service. maybe that is the problem?

            Leo
            </blockquote>

            Hi,Leo

            I put the @Name on the 2 classes this time, but it still has the same problem,
            it get exceptions in the server.log,
            as you can see the object to XML works fine, but to json, it has exceptions.
            Any ideas?
            Thanks

            2009-12-30 06:11:13,281 DEBUG [org.ajax4jsf.webapp.BaseXMLFilter] create HTML/XML parser for content type: application/xml
            2009-12-30 06:11:13,281 DEBUG [org.ajax4jsf.webapp.BaseXMLFilter] Parser not have support for the such content type, send response as-is
            2009-12-30 06:11:13,281 DEBUG [org.ajax4jsf.webapp.BaseFilter] Finished request processing total time 563ms for uri: /cuichao/seam/resource/rest/customer/bookxml
            2009-12-30 06:11:18,671 DEBUG [org.ajax4jsf.webapp.BaseFilter] Filter start request processing at 09-12-30 上午6:11  for uri: /cuichao/seam/resource/rest/customer/book
            2009-12-30 06:11:18,671 DEBUG [org.ajax4jsf.webapp.BaseFilter] Filter request output to XML
            2009-12-30 06:11:18,671 DEBUG [org.ajax4jsf.webapp.BaseXMLFilter] XML filter service start processing request
            2009-12-30 06:11:18,671 DEBUG [org.jboss.seam.resteasy.ResteasyResourceAdapter] processing REST request
            2009-12-30 06:11:18,671 DEBUG [org.jboss.seam.servlet.ContextualHttpServletRequest] beginning request
            2009-12-30 06:11:18,671 DEBUG [org.jboss.seam.contexts.ServletLifecycle] >>> Begin web request
            2009-12-30 06:11:18,687 DEBUG [org.jboss.seam.core.Manager] No stored conversation
            2009-12-30 06:11:18,718 DEBUG [org.jboss.seam.resteasy.ResteasyResourceAdapter] removing SeamResourceServlet url-pattern and dispatcher prefix from request path
            2009-12-30 06:11:18,718 DEBUG [org.jboss.seam.resteasy.ResteasyResourceAdapter] UriInfo, absolute URI       : http://localhost:8080/cuichao/seam/resource/rest/customer/book
            2009-12-30 06:11:18,718 DEBUG [org.jboss.seam.resteasy.ResteasyResourceAdapter] UriInfo, base URI           : http://localhost:8080/cuichao/
            2009-12-30 06:11:18,718 DEBUG [org.jboss.seam.resteasy.ResteasyResourceAdapter] UriInfo, relative path/@Path: /customer/book
            2009-12-30 06:11:18,718 DEBUG [org.jboss.seam.resteasy.ResteasyResourceAdapter] UriInfo, query string       : null
            2009-12-30 06:11:18,718 DEBUG [org.jboss.resteasy.core.SynchronousDispatcher] PathInfo: /customer/book
            2009-12-30 06:11:18,718 DEBUG [org.jboss.seam.resteasy.SeamResteasyResourceFactory] creating RESTEasy resource instance by looking up Seam component: myCustomerResource
            2009-12-30 06:11:18,718 INFO  [STDOUT] *****MyCustomerResource()******
            2009-12-30 06:11:18,718 INFO  [STDOUT] *****MyCustomerResource()******
            2009-12-30 06:11:18,734 INFO  [STDOUT] *****BookListing getBooksMapped()******
            2009-12-30 06:11:18,734 INFO  [STDOUT] *****BookListing getBooksMapped()******EJB 3.0
            2009-12-30 06:11:18,734 DEBUG [org.ajax4jsf.webapp.FilterServletResponseWrapper] Create ByteArrayServletOutputStream for buffer servlet output
            2009-12-30 06:11:18,734 DEBUG [org.jboss.seam.servlet.ContextualHttpServletRequest] ended request
            2009-12-30 06:11:18,734 DEBUG [org.jboss.seam.resteasy.ResteasyResourceAdapter] completed processing of REST request
            2009-12-30 06:11:18,734 DEBUG [org.jboss.seam.web.ExceptionFilter] handling uncaught exception
            javax.servlet.ServletException: Servlet execution threw an exception
                 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:313)
                 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                 at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
                 at org.jboss.seam.web.IdentityFilter.doFilter(IdentityFilter.java:40)
                 at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                 at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:90)
                 at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                 at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)
                 at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                 at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
                 at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                 at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:178)
                 at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
                 at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:390)
                 at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:517)
                 at org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:56)
                 at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                 at org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:60)
                 at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                 at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:73)
                 at org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)
                 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                 at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
                 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
                 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
                 at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
                 at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:432)
                 at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
                 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
                 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
                 at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
                 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
                 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
                 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
                 at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
                 at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
                 at java.lang.Thread.run(Thread.java:619)
            Caused by: java.lang.NoClassDefFoundError: org/codehaus/jettison/mapped/MappedNamespaceConvention
                 at org.jboss.resteasy.plugins.providers.jaxb.json.JsonJAXBContextFinder.find(JsonJAXBContextFinder.java:101)
                 at org.jboss.resteasy.plugins.providers.jaxb.json.JsonJAXBContextFinder.findCachedContext(JsonJAXBContextFinder.java:56)
                 at org.jboss.resteasy.plugins.providers.jaxb.AbstractJAXBProvider.findJAXBContext(AbstractJAXBProvider.java:53)
                 at org.jboss.resteasy.plugins.providers.jaxb.AbstractJAXBProvider.getMarshaller(AbstractJAXBProvider.java:116)
                 at org.jboss.resteasy.plugins.providers.jaxb.AbstractJAXBProvider.writeTo(AbstractJAXBProvider.java:92)
                 at org.jboss.resteasy.core.interception.MessageBodyWriterContextImpl.proceed(MessageBodyWriterContextImpl.java:114)
                 at org.jboss.resteasy.plugins.interceptors.encoding.GZIPEncodingInterceptor.write(GZIPEncodingInterceptor.java:46)
                 at org.jboss.resteasy.core.interception.MessageBodyWriterContextImpl.proceed(MessageBodyWriterContextImpl.java:120)
                 at org.jboss.resteasy.core.ServerResponse.writeTo(ServerResponse.java:184)
                 at org.jboss.resteasy.core.SynchronousDispatcher.writeJaxrsResponse(SynchronousDispatcher.java:413)
                 at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:376)
                 at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:160)
                 at org.jboss.seam.resteasy.ResteasyResourceAdapter$1.process(ResteasyResourceAdapter.java:145)
                 at org.jboss.seam.servlet.ContextualHttpServletRequest.run(ContextualHttpServletRequest.java:53)
                 at org.jboss.seam.resteasy.ResteasyResourceAdapter.getResource(ResteasyResourceAdapter.java:121)
                 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: No ClassLoaders found for: org.codehaus.jettison.mapped.MappedNamespaceConvention
                 at org.jboss.mx.loading.LoadMgr3.beginLoadTask(LoadMgr3.java:212)
                 at org.jboss.mx.loading.RepositoryClassLoader.loadClassImpl(RepositoryClassLoader.java:521)
                 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)
                 ... 57 more
            2009-12-30 06:11:18,734 DEBUG [org.jboss.seam.web.ExceptionFilter] exception root cause
            • 3. Re: Json Problem in Seam using Resteasy
              lvdberg

              Hi,




              Caused by: java.lang.NoClassDefFoundError: org/codehaus/jettison/mapped/MappedNamespaceConvention
              




              Exception states that it can't find a class, do you have the related libs on your classpath (are they included in the application). As I recall well these classes are in jettison.jar and they are included in the seam-distro.


              Leo

              • 4. Re: Json Problem in Seam using Resteasy
                Hi,

                I checked again, and it has the jettison.jar in the lib and classpath, but when I deploy the application to the wes.ear file, the jars will not exist in the wes.ear/lib or wes.ear\wes.war\WEB-INF\lib, and when I manually copy the jettison.jar into the wes.ear, it still doesn't work!
                So after that I try to use the xstream-1.3.1.jar to handle transformation between object and json, also I need to copy the jar to the wes.ear/lib manually, and it works!

                Even it works now, I still don't know why it says can not find the org/codehaus/jettison/mapped/MappedNamespaceConvention

                Anyway,thanks to Leo.
                • 5. Re: Json Problem in Seam using Resteasy
                  lvdberg

                  Hi,


                  you can always do a simple test by copying the jar into the lib-dir of the server (under Jboss -- server/default/lib ) an restart the server. If it works than you know you hava a build and/or classpath problem.


                  Leo


                  Good luck and best wishes for 2010