Jersey: Json array with 1 element is serialized as object Jersey: Json array with 1 element is serialized as object ajax ajax

Jersey: Json array with 1 element is serialized as object


I ended up using Jackson, also described in the official Jersey documentation (http://jersey.java.net/nonav/documentation/latest/user-guide.html#json.pojo.approach.section).

I had tried that before but it wasn't working because I didn't have the jackson jar in the buildpath of my project (Based on the documentation I thought it was built into jersey's core library).

I just added the jackson-all.jar file (http://wiki.fasterxml.com/JacksonDownload) and enabled the POJO support in the configuration

    <init-param>          <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>          <param-value>true</param-value>    </init-param>

And voilá!


If you were using JAXB to build JSON result, you can configure Jersey JSON procesor to get more important JSON format.

jersey official document has detailed config:

To achieve more important JSON format changes, you will need to configure Jersey JSON procesor itself. Various configuration options could be set on an JSONConfiguration instance. The instance could be then further used to create a JSONConfigurated JSONJAXBContext, which serves as a main configuration point in this area. To pass your specialized JSONJAXBContext to Jersey, you will finally need to implement a JAXBContext ContextResolver:

    @Providerpublic class JAXBContextResolver implements ContextResolver<JAXBContext> {    private final JAXBContext context;    private final Set<Class> types;    private Class[] ctypes = { FileInfo.class}; //your pojo class    public JAXBContextResolver() throws Exception {        this.types = new HashSet(Arrays.asList(ctypes));        this.context = new JSONJAXBContext(JSONConfiguration.natural().build(),                ctypes); //json configuration    }    @Override    public JAXBContext getContext(Class<?> objectType) {        return (types.contains(objectType)) ? context : null;    }}