Returning a JSON object from a REST web service with complex objects

I have disabled the web service that returns RETURN_OBJ.

However RETURN_OBJ, it itself contains several complex objects, such as listobjects from another class, maps, etc.

In that case, annotating the participating classes with @XmlRootElementand annotating the web service with is @Produces("application/json")enough?

Because just doing it does not work, and I get an error no message body writer found for class.

What is the reason, cause and solution to this error?

+5
source share
2 answers
@XmlRootElement

json xml. ex: jackson (http://jackson.codehaus.org/). xml json.

@Produces("application/json")

json, json .

+2

, ,
json, Gson Poster, url - : //_//rest/getjson? name = gopi

, , json Gson.

  @Path("rest")
public class RestImpl {

@GET
@Path("getjson")
@Produces("application/json")
public String restJson(@QueryParam("name") String name)
{
    EmployeeList employeeList = new EmployeeList();
    List<Employee> list = new ArrayList<Employee>();
    Employee e = new Employee();
    e.setName(name);
    e.setCode("1234");
    Address address = new Address();
    address.setAddress("some Address");
    e.setAddress(address);
    list.add(e);
    Employee e1 = new Employee();
    e1.setName("shankar");
    e1.setCode("54564");
    Address address1 = new Address();
    address.setAddress("Address ");
    e1.setAddress(address);
    list.add(e1);
    employeeList.setEmplList(list);

    Gson gson = new Gson();
    System.out.println(gson.toJson(employeeList));
    return gson.toJson(employeeList);

}

@GET
@Produces("text/html")
public String test()
{
    return "SUCCESS";
}

}

PS: - , -)

+5

All Articles