diff --git a/spring/src/main/java/hello/web/HelloDbController.java b/spring/src/main/java/hello/web/HelloDbController.java index fcaedd9dd0d..084c9b5762c 100644 --- a/spring/src/main/java/hello/web/HelloDbController.java +++ b/spring/src/main/java/hello/web/HelloDbController.java @@ -19,18 +19,20 @@ import org.springframework.web.bind.annotation.*; @Controller -public class HelloDbController +public class HelloDbController { + private static final int DB_ROWS = 10000; - @RequestMapping(value = "/db") - public Object index(HttpServletRequest request, HttpServletResponse response, Integer queries) + @RequestMapping(value = "/db", produces = "application/json") + @ResponseBody + public World[] index(Integer queries) { if (queries == null) { queries = 1; } - + final World[] worlds = new World[queries]; final Random random = ThreadLocalRandom.current(); final Session session = HibernateUtil.getSessionFactory().openSession(); @@ -41,17 +43,7 @@ public Object index(HttpServletRequest request, HttpServletResponse response, In } session.close(); - - try - { - new MappingJackson2HttpMessageConverter().write( - worlds, MediaType.APPLICATION_JSON, new ServletServerHttpResponse(response)); - } - catch (IOException e) - { - // do nothing - } - - return null; + + return worlds; } } diff --git a/spring/src/main/java/hello/web/HelloJsonController.java b/spring/src/main/java/hello/web/HelloJsonController.java index 8095d4b1401..8228a231627 100644 --- a/spring/src/main/java/hello/web/HelloJsonController.java +++ b/spring/src/main/java/hello/web/HelloJsonController.java @@ -11,27 +11,36 @@ import org.springframework.http.server.*; import org.springframework.stereotype.*; import org.springframework.web.bind.annotation.*; - + /** * Handles requests for the application home page. */ @Controller -public class HelloJsonController { - - @RequestMapping(value = "/json") - public Object json(HttpServletResponse response) +public class HelloJsonController +{ + + @RequestMapping(value = "/json", produces = "application/json") + @ResponseBody + public Message json() + { + return new Message("Hello, world"); + } + + public static class Message { - Map json = new HashMap(); - json.put("message", "Hello, world"); - - try { - new MappingJackson2HttpMessageConverter().write( - json, MediaType.APPLICATION_JSON, new ServletServerHttpResponse(response)); - } catch (HttpMessageNotWritableException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); + + private final String message; + + public Message(String message) + { + this.message = message; + } + + public String getMessage() + { + return message; } - return null; + } + } diff --git a/spring/src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml b/spring/src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml index b5720d659b7..989acd2054b 100644 --- a/spring/src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml +++ b/spring/src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml @@ -4,9 +4,9 @@ xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" - http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd - http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd - http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> + http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd + http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">