0

Am fetching documents from Elasticsearch via ActiveMQ using @Controller. Am calling my client method with parameter and getting response from Elastic Search as JSONArray but while am returning JSONArray from my @Controller class am getting the below error

Please find my error below.

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]

Please find my @Controller class and my @ResponseBody method

@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);
    }

}

Please find my client code implementation :

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");
}
Karthikeyan
  • 1,927
  • 6
  • 44
  • 109

1 Answers1

0

Your Jackson configuration is set to fail on empty results. From your exception:

(to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)

However, there might be other reasons. It is difficult to say without seeing the implementation of com.demo.earchengineclient.Client. If possible, try to replace JSONObject for JsonObject (the one from javax.json, that came with Java 8).

Guilherme Mussi
  • 956
  • 7
  • 14
  • I have updated my client code implementation. if am invoking my client code alone, am able to get the response in JSONArray without any issue., But if i invoke via SpringBoot `@Controller` am getting this error. – Karthikeyan Jun 10 '18 at 08:04