{"id":1553,"date":"2012-07-29T22:16:58","date_gmt":"2012-07-29T20:16:58","guid":{"rendered":"http:\/\/doanduyhai.wordpress.com\/?p=1553"},"modified":"2015-01-31T13:16:26","modified_gmt":"2015-01-31T13:16:26","slug":"gwt-json-integration-with-spring-mvc","status":"publish","type":"post","link":"https:\/\/www.doanduyhai.com\/blog\/?p=1553","title":{"rendered":"GWT JSON integration with Spring MVC"},"content":{"rendered":"<p>In the <a href=\"https:\/\/www.doanduyhai.com\/blog\/?p=153\/\" title=\"GWT RPC integration with\u00a0Spring\" target=\"_blank\">previous<\/a> post, I&#8217;ve presented GWT RPC integration with Spring. In this post we&#8217;ll see how we can achieve JSON backend service integration between GWT and Spring MVC. We&#8217;ll re-use the same StockWatcher application and change the RPC communication into JSON requests.<\/p>\n<p><!--more--><\/p>\n<h1>I Implementation<\/h1>\n<p> The integration of JSON service in the backend with Spring MVC is quite similar to the integration of RPC services with Spring plain service beans. What&#8217;s new here is the introduction of Spring MVC to drive the JSON service.<\/p>\n<p> Most of this integration is about smart configuration of the servlet mapping and the Spring MVC pipeline.<\/p>\n<p>&nbsp;<\/p>\n<h3>A Spring MVC configuration<\/h3>\n<p> Let&#8217;s see how we configure Spring MVC for this purpose:<\/p>\n<pre class=\"brush: xml; title: ; wrap-lines: false; notranslate\" title=\"\">\r\n&lt;bean class=&quot;org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping&quot; \/&gt;\r\n\t\r\n&lt;bean id=&quot;jacksonMessageConverter&quot; class=&quot;java.util.ArrayList&quot;&gt;\r\n\t&lt;constructor-arg&gt;\r\n\t\t&lt;list&gt;\r\n\t        \t&lt;bean class=&quot;org.springframework.http.converter.json.MappingJacksonHttpMessageConverter&quot; \/&gt;\r\n\t\t&lt;\/list&gt;\r\n\t&lt;\/constructor-arg&gt;   \r\n&lt;\/bean&gt;\r\n\t \r\n&lt;bean class=&quot;org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter&quot; &gt;\r\n\t&lt;property name=&quot;messageConverters&quot; ref=&quot;jacksonMessageConverter&quot;\/&gt;\r\n&lt;\/bean&gt;\r\n\r\n&lt;bean id=&quot;stockWatcherController&quot; class=&quot;com.google.gwt.sample.stockwatcher_json.controller.StockWatcherController&quot; &gt;\r\n\t&lt;property name=&quot;jsonStockDataService&quot; ref=&quot;jsonStockDataService&quot;\/&gt;\r\n\t&lt;property name=&quot;jsonRandomizeService&quot; ref=&quot;jsonRandomizeService&quot;\/&gt;\r\n&lt;\/bean&gt;\r\n<\/pre>\n<p> <strong>Yes, that&#8217;s pretty few!<\/strong> Have you noticed that we did not define any <strong>viewResolver<\/strong>? It&#8217;s perfectly normal since we&#8217;re only using Spring MVC to produce JSON data back, there is no need for a view rendering technology.<\/p>\n<p> Please notice that I prefer full XML declaration over <strong>&lt;mvc&gt;<\/strong> namespace because we have greater control with full XML config. With <strong>&lt;mvc&gt;<\/strong> namespace lots of things (message converters, exception handlers &#8230;) are injected by default (convention over configuration) and we do not need them for our usecase. <\/p>\n<p>&nbsp;<\/p>\n<h3>B web.xml configuration<\/h3>\n<p>The configuration for web.xml is very classical<\/p>\n<pre class=\"brush: xml; highlight: [16,24]; title: ; wrap-lines: false; notranslate\" title=\"\">\r\n  &lt;!-- Spring classical web application context declaration --&gt;\r\n  &lt;context-param&gt;\r\n    &lt;param-name&gt;contextConfigLocation&lt;\/param-name&gt;\r\n    &lt;param-value&gt;classpath:applicationContext.xml&lt;\/param-value&gt;\r\n  &lt;\/context-param&gt;\r\n  &lt;listener&gt;\r\n    &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;\/listener-class&gt;\r\n  &lt;\/listener&gt;\r\n   \r\n  &lt;!-- Spring MVC Servlet declaration --&gt;\r\n\t&lt;servlet&gt;\r\n\t\t&lt;servlet-name&gt;springMVCServlet&lt;\/servlet-name&gt;\r\n\t\t&lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;\/servlet-class&gt;\r\n\t\t&lt;init-param&gt;\r\n\t\t\t&lt;param-name&gt;contextConfigLocation&lt;\/param-name&gt;\r\n\t\t\t&lt;param-value&gt;classpath:stockwatcher_json-mvc.xml&lt;\/param-value&gt;\r\n\t\t&lt;\/init-param&gt;\t\r\n\t\t&lt;load-on-startup&gt;1&lt;\/load-on-startup&gt;\r\n\t&lt;\/servlet&gt;\r\n  \r\n  &lt;!-- Spring MVC Servlet mapping --&gt;\r\n  &lt;servlet-mapping&gt;\r\n    &lt;servlet-name&gt;springMVCServlet&lt;\/servlet-name&gt;\r\n    &lt;url-pattern&gt;*.json&lt;\/url-pattern&gt;\r\n  &lt;\/servlet-mapping&gt;\r\n<\/pre>\n<p>First we declare the Spring <strong>applicationContext<\/strong> as usual. <\/p>\n<p>Then we declare the Spring MVC servlet with its associated configuration file (<strong>line 16<\/strong>)<\/p>\n<p>Finally we map the MVC servlet to the <em>.jso<\/em>n extension (<strong>line 24<\/strong>). All request finishing with <em>.json<\/em> will trigger the Spring MVC servlet. The dispatching to service bean is handled by Spring MVC controller <strong>@RequestMapping<\/strong> annotation.<\/p>\n<p>&nbsp;<\/p>\n<h3>C MVC Request mapping<\/h3>\n<pre class=\"brush: java; highlight: [1,8]; title: ; wrap-lines: false; notranslate\" title=\"\">\r\n@RequestMapping(value = &quot;**\/jsonStocPrices.json&quot;, method = RequestMethod.GET, produces = &quot;application\/json&quot;)\r\n@ResponseBody\r\npublic List&lt;StockDataDto&gt; getStocks(@RequestParam(&quot;q&quot;) String rawSymbols)\r\n{\r\n\treturn this.jsonStockDataService.getStockDataFromSymboles(rawSymbols);\r\n}\r\n\r\n@RequestMapping(value = &quot;**\/jsonRandomize.json&quot;, method = RequestMethod.GET, produces = &quot;application\/json&quot;)\r\n@ResponseBody\r\npublic Integer randomize()\r\n{\r\n\treturn this.jsonRandomizeService.getRandomNumber();\r\n}\r\n<\/pre>\n<p> The request mapping in the MVC controller is quite straightforward. However you should pay attention to the request URL. Here we must use the <em>**\/<\/em> wildcard because we declared the servlet mapping as <em>*.json<\/em>.<\/p>\n<p> Indeed if the absolute URL is <em>\/StockWatcherJSON\/jsonStockPrices.json<\/em> and the declared URL in <strong>@RequestMapping<\/strong> is just <em>jsonStockPrices.json<\/em>, it will not match beacause of the leading part (<em>\/StockWatcherJSON<\/em>) of the URL.<\/p>\n<p>&nbsp;<\/p>\n<h1>II Demo<\/h1>\n<p> The demo of the above integration can be found on Github at <a href=\"https:\/\/github.com\/doanduyhai\/StockWatcherJSON\" title=\"https:\/\/github.com\/doanduyhai\/StockWatcherJSON\" target=\"_blank\">StockWatcherJSON<\/a><\/p>\n<p> All you need to do is to follow the instructions to make the Demo run.<\/p>\n<p>&nbsp;<br \/>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous post, I&#8217;ve presented GWT RPC integration with Spring. In this post we&#8217;ll see how we can achieve JSON backend service integration between GWT and Spring MVC. We&#8217;ll re-use the same StockWatcher application and change the RPC communication&#8230;<br \/><a class=\"read-more-button\" href=\"https:\/\/www.doanduyhai.com\/blog\/?p=1553\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[25,19],"tags":[],"_links":{"self":[{"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1553"}],"collection":[{"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1553"}],"version-history":[{"count":1,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1553\/revisions"}],"predecessor-version":[{"id":1755,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1553\/revisions\/1755"}],"wp:attachment":[{"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}