无法从@Controller类返回JSON数组抛出没有为类org.json.JSONObject找到的序列化程序错误

时间:2018-06-10 07:33:18

标签: java json spring elasticsearch spring-restcontroller

使用@Controller通过ActiveMQ从Elasticsearch获取文档。我使用参数调用我的客户端方法并从Elastic Search获取响应作为JSONArray但是从我的@Controller类返回JSONArray时出现以下错误

请在下面找到我的错误。

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]

请找到我的@Controller课程和我的@ResponseBody方法

@Controller
public class SearchProductWebService {
    private static final String DOCUMENTS = "/document/{name}";
    private static final Logger logger = Logger.getLogger(SearchProductWebService.class.getName());

    com.demo.earchengineclient.Client searchEngineClient = com.demo.searchengineclient.Client.getInstance();

    @RequestMapping(value = DOCUMENTS, method = RequestMethod.GET)
    public @ResponseBody Object getDocumentByName(HttpServletRequest httpRequest, HttpServletResponse httpResponse, @PathVariable("name") String name) {
        System.out.println("Search Documents--->"+searchEngineClient.searchByDocuments("arthi"));
        return searchEngineClient.searchByDocuments(name);
    }

}

请找到我的客户端代码实现:

public JSONArray searchByDocuments(String name) {
    JSONObject request = new JSONObject();
    request.put("method", "search_by_documents");
    request.put("name", name);
    this.start();
    JSONObject response = producer.post(request, 60);
    this.stop();
    return response.getJSONArray("result");
}

1 个答案:

答案 0 :(得分:0)

您的Jackson配置在空结果时设置为失败。 从你的例外:

(to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)

但是,可能还有其他原因。没有看到com.demo.earchengineclient.Client的实施就很难说。 如果可能的话,尝试用JSONObject替换JSONObject(来自javax.json的那个,与Java 8一起提供)。