How to omit @XmlRootElement from JSON output from Jersey?

I am trying to create JSON from a list of simple objects. It works, but my output is wrapped with the class name:

{"classname":[{"name":"John Doe","title":"manager"} .....]}

What I would like to have is

[{"name":"John Doe","title":"manager"} .....]

I understand this because I have @XmlRootElement in my class, but if I omit this, then I get an error:

A message body writer for Java class java.util.ArrayList, and Java type
java.util.List<MyClass>, and MIME media type application/json was not found

I set com.sun.jersey.api.json.POJOMappingFeature as true, although I'm not 100% sure if it is in the right place. I am using Embedded Jetty and I am setting a parameter for ServletHolder

+3
source share
1 answer

You can use gson to create the string of your array. which should give you the desired answer.

Gson gson = new Gson();
gson.toJson([{"name":"John Doe","title":"manager"} .....]);

: http://www.studytrails.com/java/json/java-google-json-java-to-json.jsp

javadoc: http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/Gson.html

0

All Articles