0

I am using Glassfish 3 which uses the Jersey Implementation for JAX-RS. I have the following method REST endpoint:

@GET
@Produces(MediaType.APPLICATION_JSON)
public List<CourseDTO> listAll() {

    List<CourseDTO> list = findAll();


    return list;
}

My CourseDTO is the following:

@XmlRootElement
public class CourseDTO implements Serializable {


    private long courseId;
    private String courseName;


    public CourseDTO() {

    }
     //getters setters
}

The JSON object I get back is the following:

  {
      "courseDTO":
           [
              {"courseId":"1","courseName":"C++"},
              {"courseId":"2","courseName":"Java"}
           ]
   }

However, ideally I would want the following:

[
    {"courseId":"1","courseName":"C++"},
    {"courseId":"2","courseName":"Java"}
]

So basically I want to get rid of the "wrapper" object. Is there someway to do it or should I have to do manual marshalling?

ChrisGeo
  • 3,807
  • 13
  • 54
  • 92
  • Maybe this will help you: http://stackoverflow.com/a/16614455/1817029 – My-Name-Is Dec 27 '13 at 18:55
  • Try using [Genson](http://code.google.com/p/genson/), you just need it on your classpath to enable json support with it. [Here](http://code.google.com/p/genson/wiki/JaxbBundle) are the JAXB annotations actually supported. – eugen Dec 28 '13 at 12:30

1 Answers1

1

Try using Google Gson library. The code is as simple as :

Type listType = new TypeToken<ArrayList<CourseDTO >>() {
                    }.getType();
 List<CourseDTO > courses = new Gson().fromJson(jsonArray, listType);
shahshi15
  • 2,772
  • 2
  • 20
  • 24