使用@Controller通过ActiveMQ从Elasticsearch获取记录。但是当我在Postman客户端
中收到以下错误时,我正在返回ElasticSearch响应作为JSON数组请在下面找到我的错误。
{
"timestamp": "2018-06-09T06:59:18.755+0000",
"status": 500,
"error": "Internal Server Error",
"message": "Type definition error: [simple type, class org.json.JSONObject]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.json.JSONObject and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)",
"path": "/document/40011"
}
请找到我尝试返回JSONArray的@Controller
方法
@RequestMapping(value = DOCUMENTS, method = RequestMethod.GET)
public @ResponseBody JSONArray getDocumentByName(HttpServletRequest httpRequest, HttpServletResponse httpResponse, @PathVariable("name") String name) {
System.out.println("Searching documents....");
JSONArray result;
result = searchEngineClient.searchByDocuments(name);
return result;
}
请找到堆栈跟踪:
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.json.JSONObject and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)
at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77) ~[jackson-databind-2.9.5.jar:2.9.5]
at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1191) ~[jackson-databind-2.9.5.jar:2.9.5]
at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:312) ~[jackson-databind-2.9.5.jar:2.9.5]
at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:71) ~[jackson-databind-2.9.5.jar:2.9.5]
能够以String
为以下代码返回响应。但我想以JSONArray的形式返回。
@RequestMapping(value = DOCUMENTS, method = RequestMethod.GET)
public @ResponseBody Object getDocumentByName(HttpServletRequest httpRequest, HttpServletResponse httpResponse, @PathVariable("name") String name) {
System.out.println("Searching documents....");
JSONArray result;
result = searchEngineClient.searchByDocuments(name);
return result.toString();
}
答案 0 :(得分:1)
只需删除@ResponseBody
即可。它是@RestController
,它将自动序列化为JSON。