From b80beb67de60fdcd9f9217a236159fbc53773094 Mon Sep 17 00:00:00 2001 From: Ryan Tenney Date: Thu, 28 Mar 2013 15:09:59 -0400 Subject: [PATCH 1/3] Update to spring 3.2 schemas --- .../webapp/WEB-INF/spring/appServlet/servlet-context.xml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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"> From 88255b96d8699ba9854343b59c1f6cc79a2ced95 Mon Sep 17 00:00:00 2001 From: Ryan Tenney Date: Thu, 28 Mar 2013 15:35:08 -0400 Subject: [PATCH 2/3] Improve controllers Let spring handle serializing and writing the response body. --- .../java/hello/web/HelloDbController.java | 24 ++++++----------- .../java/hello/web/HelloJsonController.java | 27 +++++++------------ 2 files changed, 18 insertions(+), 33 deletions(-) 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..8cc43c27f75 100644 --- a/spring/src/main/java/hello/web/HelloJsonController.java +++ b/spring/src/main/java/hello/web/HelloJsonController.java @@ -11,27 +11,20 @@ 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) - { - Map json = new HashMap(); - json.put("message", "Hello, world"); +public class HelloJsonController +{ - try { - new MappingJackson2HttpMessageConverter().write( - json, MediaType.APPLICATION_JSON, new ServletServerHttpResponse(response)); - } catch (HttpMessageNotWritableException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - return null; + @RequestMapping(value = "/json", produces = "application/json") + @ResponseBody + public Map json() + { + Map map = new HashMap(); + map.put("message", "Hello, world"); + return map; } } From 295137f19c7ca68137984732db397a9dbb43d9f2 Mon Sep 17 00:00:00 2001 From: Ryan Tenney Date: Thu, 28 Mar 2013 15:41:47 -0400 Subject: [PATCH 3/3] Compare apples to apples Json controller now replies with a Message object instead of a Map. Creating tons of maps is memory intensive, plus this tests Jackson's ability to serialize a bean instead of a Map. --- .../java/hello/web/HelloJsonController.java | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/spring/src/main/java/hello/web/HelloJsonController.java b/spring/src/main/java/hello/web/HelloJsonController.java index 8cc43c27f75..8228a231627 100644 --- a/spring/src/main/java/hello/web/HelloJsonController.java +++ b/spring/src/main/java/hello/web/HelloJsonController.java @@ -21,10 +21,26 @@ public class HelloJsonController @RequestMapping(value = "/json", produces = "application/json") @ResponseBody - public Map json() + public Message json() { - Map map = new HashMap(); - map.put("message", "Hello, world"); - return map; + return new Message("Hello, world"); } + + public static class Message + { + + private final String message; + + public Message(String message) + { + this.message = message; + } + + public String getMessage() + { + return message; + } + + } + }