0

I have restful web service which return list of users, i want to make response as json format but that produce the following exception:

    SEVERE: Servlet.service() for servlet [RESTful] in context with path [/spring] threw exception
org.codehaus.jackson.map.JsonMappingException: No serializer found for class org.json.JSONObject and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) )

my restful method:

@GET
 @Path("all")
 @Produces(MediaType.APPLICATION_JSON)
 public Response getUsers(){
     UserService service = new UserService();
     List<UserBean> userBeans = service.getUsers();
     JSONObject users = new JSONObject();

     if(userBeans != null)
     {
         for(UserBean user : userBeans)
         {
             users.put("name",user.getUsername());
         }

         System.out.println(users);
         return Response.status(200).entity(users).build(); 
     }
     return Response.status(201).entity("faild").build();   
   }
cassiomolin
  • 124,154
  • 35
  • 280
  • 359
Ola Zeyad
  • 113
  • 1
  • 1
  • 9

1 Answers1

0

To produce JSON with Jersey, you don't need to use the org.json.JSONObject class.

Ensure you have a JSON provider configured (refer to this answer for more details) and change your resource method to be as following:

@GET
@Path("all")
@Produces(MediaType.APPLICATION_JSON)
public Response getUsers() {

    UserService service = new UserService();
    List<UserBean> users = service.getUsers();

    return Response.ok(users).build();
}
Community
  • 1
  • 1
cassiomolin
  • 124,154
  • 35
  • 280
  • 359