{"id":12686,"date":"2013-05-10T16:00:42","date_gmt":"2013-05-10T13:00:42","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=12686"},"modified":"2013-05-11T01:51:36","modified_gmt":"2013-05-10T22:51:36","slug":"spring-mvc-ajax-and-json-part-2-the-server-side-code","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2013\/05\/spring-mvc-ajax-and-json-part-2-the-server-side-code.html","title":{"rendered":"Spring MVC, Ajax and JSON Part 2 &#8211; The Server Side Code"},"content":{"rendered":"<p>In <a href=\"http:\/\/www.javacodegeeks.com\/2013\/05\/spring-mvc-ajax-and-json-part-1-setting-the-scene-2.html\" target=\"new\">my last blog<\/a> I said that I was going to talk about Spring, Ajax and JSON, but didn&#8217;t. The reason for this is that I wanted to set the scene using a (barely) credible shopping web site scenario. In this scenario when the user clicks on the eCommerce page link, the server app loads some the items from a catalogue and displays them on the page. The user then checks a number of items and presses &#8216;Confirm Purchase&#8217;. Now, this is where Ajax and JSON come in, on pressing &#8216;Confirm Purchase&#8217; the browser makes an Ajax request to the server sending it the item ids. The server then retrieves the items from the database returns them as JSON to the browser. The browser then processes the JSON, displaying the items on he screen.<\/p>\n<p> My <a href=\"http:\/\/www.javacodegeeks.com\/2013\/05\/spring-mvc-ajax-and-json-part-1-setting-the-scene-2.html\" target=\"new\">last blog<\/a> got as far as creating and displaying a form that presented a list of items<br \/>\n&nbsp;<br \/>\nfrom the imaginary catalogue to the user. This blog takes a look at the next step in the project: creating some JSON.<br \/>\n<a name=\"more\"><\/a><\/p>\n<p> The Guys at Spring have been busy working on Ajax and JSON over the last couple of years and, as you\u2019d expect, they do a lot of the work for you in the background. This means that all you need to do is to define a simple bean class that Spring can turn into JSON and write some controller code. In this case that class that Spring will convert to JSON is the <code>OrderForm<\/code> class:<\/p>\n<pre class=\"brush:java\">public class OrderForm { \r\n \r\n&nbsp; private final List&lt;Item&gt; items; \r\n \r\n&nbsp; private final String purchaseId; \r\n \r\n&nbsp; public OrderForm(List&lt;Item&gt; items, String purchaseId) { \r\n&nbsp;&nbsp;&nbsp; super(); \r\n&nbsp;&nbsp;&nbsp; this.items = items; \r\n&nbsp;&nbsp;&nbsp; this.purchaseId = purchaseId; \r\n&nbsp; } \r\n \r\n&nbsp; public List&lt;Item&gt; getItems() { \r\n&nbsp;&nbsp;&nbsp; return items; \r\n&nbsp; } \r\n \r\n&nbsp; public String getPurchaseId() { \r\n&nbsp;&nbsp;&nbsp; return purchaseId; \r\n&nbsp; } \r\n}<\/pre>\n<p> The <code>OrderForm<\/code> class contains a list of <code>Item<\/code> objects and a unique order ID used to define an order.<\/p>\n<p> Having created the <code>OrderForm<\/code>, the next thing to do is to sort out the Spring controller code:<\/p>\n<pre class=\" brush:java\">&nbsp; public @ResponseBody \r\n&nbsp; OrderForm confirmPurchases(@ModelAttribute(&quot;userSelections&quot;) UserSelections userSelections) { \r\n \r\n&nbsp;&nbsp;&nbsp; logger.debug(&quot;Confirming purchases...&quot;); \r\n&nbsp;&nbsp;&nbsp; OrderForm orderForm = createOrderForm(userSelections.getSelection()); \r\n&nbsp;&nbsp;&nbsp; return orderForm; \r\n&nbsp; } \r\n \r\n&nbsp; private OrderForm createOrderForm(List&lt;String&gt; selections) { \r\n \r\n&nbsp;&nbsp;&nbsp; List&lt;Item&gt; items = findItemsInCatalogue(selections); \r\n&nbsp;&nbsp;&nbsp; String purchaseId = getPurchaseId(); \r\n \r\n&nbsp;&nbsp;&nbsp; OrderForm orderForm = new OrderForm(items, purchaseId); \r\n&nbsp;&nbsp;&nbsp; return orderForm; \r\n&nbsp; } \r\n \r\n&nbsp; private List&lt;Item&gt; findItemsInCatalogue(List&lt;String&gt; selections) { \r\n \r\n&nbsp;&nbsp;&nbsp; List&lt;Item&gt; items = new ArrayList&lt;Item&gt;(); \r\n&nbsp;&nbsp;&nbsp; for (String selection : selections) { \r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Item item = catalogue.findItem(Integer.valueOf(selection)); \r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; items.add(item); \r\n&nbsp;&nbsp;&nbsp; } \r\n&nbsp;&nbsp;&nbsp; return items; \r\n&nbsp; } \r\n \r\n&nbsp; private String getPurchaseId() { \r\n&nbsp;&nbsp;&nbsp; return UUID.randomUUID().toString(); \r\n&nbsp; }<\/pre>\n<p> The code above is all that&#8217;s required to return some JSON to the browser and you can see that there\u2019s not that much to it. Firstly, the method\u2019s  <code>@RequestMapping<\/code> annotation, using the  <code>confirm<\/code> and <code>RequestMethod.POST<\/code> values, maps the form attributes from my<br \/>\n<a href=\"http:\/\/www.captaindebug.com\/2013\/04\/spring-mvc-ajax-and-json-part-1-setting.html\" target=\"new\">previous blog<\/a> to this method.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<pre class=\" brush:xml\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;form:form modelAttribute=&quot;userSelections&quot; action=&quot;confirm&quot; method=&quot;post&quot;&gt;<\/pre>\n<p> The <code>modelAttribute<\/code> annotation tells Spring to create and map a <code>userSelections<\/code> object from the forms posted data and inject it into the <code>confirmPurchases(...)<\/code> method&#8217;s <code>userSelections<\/code> argument. The <code>UserSelections<\/code> class is a convenience class that wraps a list of <code>String<\/code>s. Although an example of the Lazy Class anti-pattern, this class is used to effortlessly integrate with Spring\u2019s <code>&lt;form:checkbox&gt;<\/code> tag and in a real world application would contain more attributes.<\/p>\n<pre class=\" brush:java\">public class UserSelections { \r\n \r\n&nbsp; private List&lt;String&gt; selection = Collections.emptyList(); \r\n \r\n&nbsp; public List&lt;String&gt; getSelection() { \r\n&nbsp;&nbsp;&nbsp; return selection; \r\n&nbsp; } \r\n \r\n&nbsp; public void setSelection(List&lt;String&gt; selection) { \r\n&nbsp;&nbsp;&nbsp; this.selection = selection; \r\n&nbsp; } \r\n \r\n&nbsp; @Override \r\n&nbsp; public String toString() { \r\n \r\n&nbsp;&nbsp;&nbsp; StringBuilder sb = new StringBuilder(&quot;Selections are: &quot;); \r\n \r\n&nbsp;&nbsp;&nbsp; for (String str : selection) { \r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sb.append(str); \r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sb.append(&quot;,&nbsp; &quot;); \r\n&nbsp;&nbsp;&nbsp; } \r\n \r\n&nbsp;&nbsp;&nbsp; return sb.toString(); \r\n&nbsp; } \r\n}<\/pre>\n<p> The <code>confirmPurchases(...)<\/code> method converts a <code>UserSelections<\/code> input object into an <code>OrderForm<\/code> output object that&#8217;s passed back to the browser as JSON. The <code>OrderForm<\/code> object is created by looping through the list of <code>Item<\/code> ids held in the <code>UserSelection<\/code> object and looking up the corresponding <code>Item<\/code>s using the fake <code>catalogue<\/code> service. Once it has the list of <code>Item<\/code>s it then creates a unique purchase id using Java&#8217;s <code>UUID<\/code> class. It then passes the <code>Item<\/code>s list and the purchase ID to the <code>OrderForm<\/code>&#8216;s constructor and the order form is then passed back to Spring. Don&#8217;t forget the <code>@ResposeBody<\/code> annotation, which tells Spring to bind the <code>OrderForm<\/code> to the HTTP response body using a suitable <code>HttpMessageConverter<\/code>. This is where the magic comes in. As you may guess, a HTTP response body needs to include data that is of the correct media type to send over the internet and <code>OrderForm<\/code> definitely doesn&#8217;t fit that bill. To fix the problem it seems that Spring takes a look at the project config for suitable ways of converting the <code>OrderForm<\/code> object where it finds the <code>jackson-core<\/code> and <code>jackson-databind<\/code> libraries that were added to the project in the <a href=\"http:\/\/www.captaindebug.com\/2013\/04\/spring-mvc-ajax-and-json-part-1-setting.html\" target=\"new\">last blog<\/a>. <\/p>\n<pre class=\" brush:xml\">&lt;dependency&gt;\r\n   &lt;groupId&gt;com.fasterxml.jackson.core&lt;\/groupId&gt;\r\n   &lt;artifactId&gt;jackson-core&lt;\/artifactId&gt;\r\n   &lt;version&gt;2.0.4&lt;\/version&gt;\r\n  &lt;\/dependency&gt;\r\n  &lt;dependency&gt;\r\n   &lt;groupId&gt;com.fasterxml.jackson.core&lt;\/groupId&gt;\r\n   &lt;artifactId&gt;jackson-databind&lt;\/artifactId&gt;\r\n   &lt;version&gt;2.0.4&lt;\/version&gt;\r\n  &lt;\/dependency&gt;<\/pre>\n<p> In the absence of any other suitable candidates, uses these libraries to convert the <code>OrderForm<\/code> object to JSON. All of which means that you and I don&#8217;t actually have to do any real coding to produce our JSON output. Pretty clever huh!<\/p>\n<p> Obviously, all this magical jiggery-pokery that&#8217;s going on in the background hides away the actual JSON output so, I find it useful to create a simple unit test similar to the one shown below:<\/p>\n<pre class=\" brush:java\">&nbsp; @Test \r\n&nbsp; public void testDemonstrateJSON() throws JsonGenerationException, JsonMappingException, IOException { \r\n \r\n&nbsp;&nbsp;&nbsp; UserSelections userSelection = new UserSelections(); \r\n&nbsp;&nbsp;&nbsp; String[] selections = { &quot;1&quot;, &quot;2&quot; }; \r\n&nbsp;&nbsp;&nbsp; userSelection.setSelection(Arrays.asList(selections)); \r\n \r\n&nbsp;&nbsp;&nbsp; Item item1 = Item.getInstance(1, &quot;name&quot;, &quot;description&quot;, new BigDecimal(&quot;1.00&quot;)); \r\n&nbsp;&nbsp;&nbsp; when(catalogue.findItem(1)).thenReturn(item1); \r\n&nbsp;&nbsp;&nbsp; Item item2 = Item.getInstance(2, &quot;name2&quot;, &quot;description2&quot;, new BigDecimal(&quot;2.00&quot;)); \r\n&nbsp;&nbsp;&nbsp; when(catalogue.findItem(2)).thenReturn(item2); \r\n \r\n&nbsp;&nbsp;&nbsp; OrderForm orderForm = instance.confirmPurchases(userSelection); \r\n \r\n&nbsp;&nbsp;&nbsp; ObjectMapper mapper = new ObjectMapper(); \r\n&nbsp;&nbsp;&nbsp; String result = mapper.writeValueAsString(orderForm); \r\n \r\n&nbsp;&nbsp;&nbsp; System.out.println(result); \r\n&nbsp; }<\/pre>\n<p> You may argue that this isn&#8217;t a real test as it doesn&#8217;t assert anything. The value of this test is to give a visual representation of the JSON output and to ensure that the object you&#8217;re attaching to the HTTP response body can be converted into JSON by the Jackson parser. If it can&#8217;t then when you run this test you&#8217;ll get an exception.<\/p>\n<p> So, that\u2019s the server side code covered. The next, and hopefully last, blog in this short series takes a look at the client side code. For the full source code to this blog, see GitHub &#8211; <a href=\"https:\/\/github.com\/roghughe\/captaindebug\/tree\/master\/ajax-json\" target=\"new\">https:\/\/github.com\/roghughe\/captaindebug\/tree\/master\/ajax-json<\/a><\/p>\n<p>\n&nbsp;<\/p>\n<div style=\"border:1px solid #D8D8D8;background: #FAFAFA;width:100%;padding-left:5px\"><b><i>Reference: <\/i><\/b><a href=\"http:\/\/www.captaindebug.com\/2013\/05\/spring-mvc-ajax-and-json-part-2-server.html\">Spring MVC, Ajax and JSON Part 2 &#8211; The Server Side Code<\/a> from our <a href=\"http:\/\/www.javacodegeeks.com\/jcg\">JCG partner<\/a> Roger Hughes at the <a href=\"http:\/\/www.captaindebug.com\/\">Captain Debug&#8217;s Blog <\/a> blog.<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In my last blog I said that I was going to talk about Spring, Ajax and JSON, but didn&#8217;t. The reason for this is that I wanted to set the scene using a (barely) credible shopping web site scenario. In this scenario when the user clicks on the eCommerce page link, the server app loads &hellip;<\/p>\n","protected":false},"author":65,"featured_media":175,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[353,69,30,150],"class_list":["post-12686","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-enterprise-java","tag-ajax","tag-json","tag-spring","tag-spring-mvc"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Spring MVC, Ajax and JSON Part 2 - The Server Side Code<\/title>\n<meta name=\"description\" content=\"In my last blog I said that I was going to talk about Spring, Ajax and JSON, but didn&#039;t. The reason for this is that I wanted to set the scene using a\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2013\/05\/spring-mvc-ajax-and-json-part-2-the-server-side-code.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Spring MVC, Ajax and JSON Part 2 - The Server Side Code\" \/>\n<meta property=\"og:description\" content=\"In my last blog I said that I was going to talk about Spring, Ajax and JSON, but didn&#039;t. The reason for this is that I wanted to set the scene using a\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2013\/05\/spring-mvc-ajax-and-json-part-2-the-server-side-code.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2013-05-10T13:00:42+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2013-05-10T22:51:36+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/json-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Roger Hughes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Roger Hughes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/05\\\/spring-mvc-ajax-and-json-part-2-the-server-side-code.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/05\\\/spring-mvc-ajax-and-json-part-2-the-server-side-code.html\"},\"author\":{\"name\":\"Roger Hughes\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/c9feacaf8e783104a69621cd65bf1f07\"},\"headline\":\"Spring MVC, Ajax and JSON Part 2 &#8211; The Server Side Code\",\"datePublished\":\"2013-05-10T13:00:42+00:00\",\"dateModified\":\"2013-05-10T22:51:36+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/05\\\/spring-mvc-ajax-and-json-part-2-the-server-side-code.html\"},\"wordCount\":804,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/05\\\/spring-mvc-ajax-and-json-part-2-the-server-side-code.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/json-logo.jpg\",\"keywords\":[\"Ajax\",\"JSON\",\"Spring\",\"Spring MVC\"],\"articleSection\":[\"Enterprise Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/05\\\/spring-mvc-ajax-and-json-part-2-the-server-side-code.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/05\\\/spring-mvc-ajax-and-json-part-2-the-server-side-code.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/05\\\/spring-mvc-ajax-and-json-part-2-the-server-side-code.html\",\"name\":\"Spring MVC, Ajax and JSON Part 2 - The Server Side Code\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/05\\\/spring-mvc-ajax-and-json-part-2-the-server-side-code.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/05\\\/spring-mvc-ajax-and-json-part-2-the-server-side-code.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/json-logo.jpg\",\"datePublished\":\"2013-05-10T13:00:42+00:00\",\"dateModified\":\"2013-05-10T22:51:36+00:00\",\"description\":\"In my last blog I said that I was going to talk about Spring, Ajax and JSON, but didn't. The reason for this is that I wanted to set the scene using a\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/05\\\/spring-mvc-ajax-and-json-part-2-the-server-side-code.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/05\\\/spring-mvc-ajax-and-json-part-2-the-server-side-code.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/05\\\/spring-mvc-ajax-and-json-part-2-the-server-side-code.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/json-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/json-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/05\\\/spring-mvc-ajax-and-json-part-2-the-server-side-code.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Enterprise Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/enterprise-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Spring MVC, Ajax and JSON Part 2 &#8211; The Server Side Code\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/c9feacaf8e783104a69621cd65bf1f07\",\"name\":\"Roger Hughes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/db9d1e5362dbc3f8007b383b852473b59fb8c5282a6066a13ab1cef761a9d5d6?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/db9d1e5362dbc3f8007b383b852473b59fb8c5282a6066a13ab1cef761a9d5d6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/db9d1e5362dbc3f8007b383b852473b59fb8c5282a6066a13ab1cef761a9d5d6?s=96&d=mm&r=g\",\"caption\":\"Roger Hughes\"},\"sameAs\":[\"http:\\\/\\\/www.captaindebug.com\\\/\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/Roger-Hughes\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Spring MVC, Ajax and JSON Part 2 - The Server Side Code","description":"In my last blog I said that I was going to talk about Spring, Ajax and JSON, but didn't. The reason for this is that I wanted to set the scene using a","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2013\/05\/spring-mvc-ajax-and-json-part-2-the-server-side-code.html","og_locale":"en_US","og_type":"article","og_title":"Spring MVC, Ajax and JSON Part 2 - The Server Side Code","og_description":"In my last blog I said that I was going to talk about Spring, Ajax and JSON, but didn't. The reason for this is that I wanted to set the scene using a","og_url":"https:\/\/www.javacodegeeks.com\/2013\/05\/spring-mvc-ajax-and-json-part-2-the-server-side-code.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2013-05-10T13:00:42+00:00","article_modified_time":"2013-05-10T22:51:36+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/json-logo.jpg","type":"image\/jpeg"}],"author":"Roger Hughes","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Roger Hughes","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2013\/05\/spring-mvc-ajax-and-json-part-2-the-server-side-code.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/05\/spring-mvc-ajax-and-json-part-2-the-server-side-code.html"},"author":{"name":"Roger Hughes","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/c9feacaf8e783104a69621cd65bf1f07"},"headline":"Spring MVC, Ajax and JSON Part 2 &#8211; The Server Side Code","datePublished":"2013-05-10T13:00:42+00:00","dateModified":"2013-05-10T22:51:36+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/05\/spring-mvc-ajax-and-json-part-2-the-server-side-code.html"},"wordCount":804,"commentCount":0,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/05\/spring-mvc-ajax-and-json-part-2-the-server-side-code.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/json-logo.jpg","keywords":["Ajax","JSON","Spring","Spring MVC"],"articleSection":["Enterprise Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2013\/05\/spring-mvc-ajax-and-json-part-2-the-server-side-code.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2013\/05\/spring-mvc-ajax-and-json-part-2-the-server-side-code.html","url":"https:\/\/www.javacodegeeks.com\/2013\/05\/spring-mvc-ajax-and-json-part-2-the-server-side-code.html","name":"Spring MVC, Ajax and JSON Part 2 - The Server Side Code","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/05\/spring-mvc-ajax-and-json-part-2-the-server-side-code.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/05\/spring-mvc-ajax-and-json-part-2-the-server-side-code.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/json-logo.jpg","datePublished":"2013-05-10T13:00:42+00:00","dateModified":"2013-05-10T22:51:36+00:00","description":"In my last blog I said that I was going to talk about Spring, Ajax and JSON, but didn't. The reason for this is that I wanted to set the scene using a","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/05\/spring-mvc-ajax-and-json-part-2-the-server-side-code.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2013\/05\/spring-mvc-ajax-and-json-part-2-the-server-side-code.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2013\/05\/spring-mvc-ajax-and-json-part-2-the-server-side-code.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/json-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/json-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2013\/05\/spring-mvc-ajax-and-json-part-2-the-server-side-code.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Enterprise Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/enterprise-java"},{"@type":"ListItem","position":4,"name":"Spring MVC, Ajax and JSON Part 2 &#8211; The Server Side Code"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/c9feacaf8e783104a69621cd65bf1f07","name":"Roger Hughes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/db9d1e5362dbc3f8007b383b852473b59fb8c5282a6066a13ab1cef761a9d5d6?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/db9d1e5362dbc3f8007b383b852473b59fb8c5282a6066a13ab1cef761a9d5d6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/db9d1e5362dbc3f8007b383b852473b59fb8c5282a6066a13ab1cef761a9d5d6?s=96&d=mm&r=g","caption":"Roger Hughes"},"sameAs":["http:\/\/www.captaindebug.com\/"],"url":"https:\/\/www.javacodegeeks.com\/author\/Roger-Hughes"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/12686","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=12686"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/12686\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/175"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=12686"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=12686"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=12686"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}