【发布时间】:2021-01-05 16:20:41
【问题描述】:
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getData(@QueryParam("id") long id) throws Exception {
JSONArray json = (getting some json data from db)
ObjectMapper obj = new ObjectMapper();
return Response.ok(obj.writeValueAsString(json)).build();
}
我试图像这样返回 json 数组,但得到这样的错误。
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.json.JSONArray and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)
谁能帮我序列化这个并提供响应。提前致谢。
【问题讨论】:
-
看看与另一个 3rd 方库 How to return N1qlQueryResult as the response of REST API for Couchbase databse? 类似的问题。您无需手动将对象转换为
JSON。MVC层应该为您做到这一点。如果没有,必须有办法,检查文档。如果您没有POJO,您始终可以创建一个Map来表示JSON Object和List或array来表示JSON Array。 -
您需要使用 ObjectMapper 注册json-org module。
标签: java jackson jersey-2.0 jackson2