4

I am using Jersey+Jackon to make a REST API which works with JSON.

Assume that I have a class as follows:

@XmlRootElement
public class A {
    public String s;
}

and here is my jersey method which uses the class:

@GET
@Produces(MediaType.APPLICATION_JSON)
public Object get(@PathParam("id") String id) throws Exception{
    A[] a= new A[2];
    a[0] = new A();
    a[0].s="abc";
    a[1] = new A();
    a[1].s="def";
    return a;
}

the out put is:

{"a":[{"s":"abc"},{"s":"def"}]}

but I want it to be like this:

[{"s":"abc"},{"s":"def"}]

What should I do? Please help me.

Morteza
  • 341
  • 5
  • 13

3 Answers3

2

Your requirement seems to be to drop the root element from json string. This can be configured in Jersey as follows. In Jersey, whether dropping root element is configured by JSONConfiguration.rootUnwrapping(). More details can be found in JSON support in Jersey and CXF.

Here's a sample code that does this.

   @Provider
   public class MyJAXBContextResolver implements ContextResolver<JAXBContext> {

       private JAXBContext context;
       private Class[] types = {StatusInfoBean.class, JobInfoBean.class};

       public MyJAXBContextResolver() throws Exception {
           this.context = new JSONJAXBContext(
                   JSONConfiguration.mapped()
                                      .rootUnwrapping(true)
                                      .arrays("jobs")
                                      .nonStrings("pages", "tonerRemaining")
                                      .build(),
                   types);
       }

       public JAXBContext getContext(Class<?> objectType) {
           return (types[0].equals(objectType)) ? context : null;
       }
   }
Kasun Gajasinghe
  • 2,735
  • 1
  • 21
  • 30
  • I am new to Jersey+Jackson. would you let me know how I should use this code? – Morteza Jun 14 '12 at 06:32
  • As you see, this is a `@Provider` in JAX-RS vocabulary. So, register this as a provider. – Kasun Gajasinghe Jun 14 '12 at 06:44
  • I understand this process is little complicated in Jersey. On CXF, configuration is pretty easy. There, you can simply configure the default json provider that comes with CXF via a beans file. There's plenty of samples in the internet on how the beans are used. – Kasun Gajasinghe Jun 14 '12 at 06:48
  • Here's an example on how to use @Provider in Jersey. http://goo.gl/WUtIz and http://goo.gl/dh05f – Kasun Gajasinghe Jun 14 '12 at 06:58
  • A valid XML document must contain a root element. So, I'm not sure about your requirement here. Anyway, I'm not sure whether there a way to do that. Probably not. – Kasun Gajasinghe Nov 22 '12 at 02:42
0

The first one is a valid JSON String.

The second is not.

Subir Kumar Sao
  • 8,171
  • 3
  • 26
  • 47
0

You could try add the following to your web.xml, under the relevant <servlet> node:

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

The Jersey documentation has more information on this setting.

This feature was introduced in Jersey 1.4 and depends on Jackson. If you're using the version bundled with Glassfish 3.0.1 or below, you will need to follow the upgrade instructions.

Ashley Ross
  • 2,345
  • 2
  • 23
  • 43
  • for more details "and depends on Jackson" (jersey-media-json-jackson dependency) here: http://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-json-jackson – Ninja Coding May 16 '16 at 16:27
  • here how to register jackson to jersey: https://jersey.java.net/nonav/documentation/latest/user-guide.html#jackson-registration – Ninja Coding May 16 '16 at 16:33