0

i have a method returning json object. I have used annotation based configuration and annotated the method as @ResponseBody. I get the correct output if i return the object as a string. But i need the json formatted object to be returned. Since I'm new to spring I'm stuck fixing this error

HTTP Status 500 – Internal Server Error Type Exception Report

Message No converter found for return value of type: class org.json.simple.JSONObject

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class org.json.simple.JSONObject org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:218) org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:182) org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:82) org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:119) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:871) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:777) org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:870) javax.servlet.http.HttpServlet.service(HttpServlet.java:634) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:855) javax.servlet.http.HttpServlet.service(HttpServlet.java:741) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) Note The full stack trace of the root cause is available in the server logs.

Apache Tomcat/9.0.10 How do i fix this?

jzz
  • 17
  • 2
  • Have you tried just returning the original object? See https://stackoverflow.com/questions/44839753/returning-json-object-as-response-in-spring-boot – Ryan Dawson Aug 20 '18 at 13:18
  • i tried returning original object which is a json object. I got this error returning json object. – jzz Aug 20 '18 at 13:26
  • Are you building that JSONObject? It would be good to include a code snippet if you can. – Ryan Dawson Aug 20 '18 at 13:36
  • JSONObject json = new JSONObject(); JSONArray jsonArray = new JSONArray(); ResultSet rs=stmt.executeQuery("select * from emp"); while(rs.next()) { JSONObject obj = new JSONObject(); obj.put("id",product.getId()); obj.put("name", product.getName()); jsonArray.add(obj); } json.put("Emp",jsonArray); – jzz Aug 20 '18 at 13:45
  • Why dont you just return a dto? – mrkernelpanic Aug 20 '18 at 13:52
  • Yeah a dto or do like https://stackoverflow.com/a/44839954/9705485 – Ryan Dawson Aug 20 '18 at 14:02
  • Do you have `jackson` in your dependencies ? – Thoomas Aug 20 '18 at 14:55
  • yea i have jackson in dependency – jzz Aug 21 '18 at 04:16

0 Answers0