I have a Books class with ArrayList of BookMeta objects , the name of the arraylist is bookList.
If the bookList holds more than one BookMeta, it works good for the clients as they receive an array. But if there is only one BookMeta object in bookList, they dont receive an array, instead they get one element. I took the following workaround for forcing the output to be one array :
@Provider
public class JaxbContextResolver implements ContextResolver<JAXBContext> {
private JAXBContext context;
private Class[] types = {BookMeta.class, Books.class};
public JaxbContextResolver() throws Exception {
this.context = new JSONJAXBContext( JSONConfiguration.mapped().arrays("bookList").build(), types);
}
public JAXBContext getContext(Class<?> objectType) {
for (Class type : types) {
if (type == objectType) {
return context;
}
}
return null;
}
}
Still I am not getting arrays. What else should I do ? Do I need to explicitly register the Provider classes ? I read somewhere they are automatically detected.
Thanks.
Edit : I am using Tomcat 6, and my web.xml looks like :
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>resources</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
Got it working…had done a stupid mistake – the provider classes have to be kept in the resources folder (with property com.sun.jersey.config.property.packages) configured in web.xml
Thanks for the help !