我有宁静的Web服务,返回用户列表,我想以json格式进行响应,但产生以下异常:
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) )
我的安慰方法:
@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();
}
答案 0 :(得分:0)
要使用Jersey生成JSON,您不需要使用org.json.JSONObject
类。
确保您已配置JSON提供程序(有关详细信息,请参阅this answer)并将资源方法更改为:
@GET
@Path("all")
@Produces(MediaType.APPLICATION_JSON)
public Response getUsers() {
UserService service = new UserService();
List<UserBean> users = service.getUsers();
return Response.ok(users).build();
}