Page 1 of 1

Wrapper class not known to the context - Rest API

Posted: Thu Jun 26, 2014 4:07 pm
by gowthamgutha
I haven't modified any thing related to rest-api in the site module. Part of the web.xml is like this..

Code: Select all

    <servlet>
       <servlet-name>RESTApiServlet</servlet-name>
       <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.mycompany.api</param-value>
        </init-param>
   </servlet>

   <servlet-mapping>
       <servlet-name>RESTApiServlet</servlet-name>
       <url-pattern>/api/v1/*</url-pattern>
   </servlet-mapping>



I am able to access the endpoints defined by the broadleaf like..

Code: Select all

http://localhost:9009/api/v1/catalog/search/products?q=go


I haven't created any new endpoints, but I have added some methods to the com.mycompany.api.endpoint.catalog.CatalogEndpoint and I have created a wrapper class in the same package.

The method looks something like this..

Code: Select all

public List<MyEntityWrapper> findMyEntitys(@Context HttpServletRequest request,
            @PathParam("id") Long id) {
       
       myDao.setEntityManager(em);
       
       List<MyEntity> MyEntitys=myDao.findAllMyEntitysOrderByRating(id);
       
       if(MyEntitys==null || MyEntitys.size()>0)
       {
       ArrayList<MyEntityWrapper> wrappers=new ArrayList<MyEntityWrapper>();
       
          for(MyEntity MyEntity:MyEntitys)
          {
              MyEntityWrapper sw=new MyEntityWrapper(myDao.findMyEntityProductFor(id, MyEntity.getMyEntityId()));
              sw.wrapSummary(MyEntity, request);
              wrappers.add(sw);
          }
       
       return wrappers;
       }
       
    return null;
    }


I am getting the following exception when accessing the endpoint..

Code: Select all

javax.ws.rs.WebApplicationException: javax.xml.bind.MarshalException
 - with linked exception:
[com.sun.istack.internal.SAXException2: unable to marshal type "com.mycompany.api.endpoint.catalog.MyEntityWrapper" as an element because it is not known to this context.]

Re: Wrapper class not known to the context - Rest API

Posted: Thu Jun 26, 2014 4:14 pm
by phillipuniverse
What does the MyEntityWrapper class look like?

Re: Wrapper class not known to the context - Rest API

Posted: Fri Jun 27, 2014 2:55 pm
by gowthamgutha
It extends BaseWrapper and implements APIWrapper<MyEntity>

Thanks for the reply. I have solved the problem by including the following something like this in the applicationContext-rest-api.xml in the site module.

Code: Select all

<bean class="com.mycompany.api.endpoint.catalog.MyEntityWrapper"/>
and instead of directly creating MyEntityWrapper object directly, i got it from the

Code: Select all

context.getBean("com.mycompany.api.endpoint.catalog.MyEntityWrapper")