JSON MVC

MVC framework allows the JsonResult for Controller actions. jQuery received a huge surge of interest of the ASP.NET MVC community. Getting an AJAX request with jQuery and JSON in MVC is actually pretty simple. The first step is to have a Controller action that returns a JsonResult:

public JsonResult GetStateList()
{
List<ListItem> list = new List<ListItem>() {
new ListItem() { Value = “1”, Text = “VA” },
new ListItem() { Value = “2”, Text = “MD” },
new ListItem() { Value = “3”, Text = “DC” }
};
return this.Json(list);
}

Steps used to output JSON data from Spring MVC: – 1) Project dependencies: – To use JSON in spring MVC we include the property called Jackson dependency. Eg: –

 <properties>
<spring.version>3.0.5.RELEASE</spring.version>
</properties>
<dependencies>

<!– Jackson JSON Mapper –>

 <dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.7.1</version>
</dependency>
<!-- Spring 3 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>

 

2) Model: – The Customer Inquiry Controller method makes a call to an application service that returns a Customer View Model of customer data. In MVC, the Model is defined as a collection of classes that define your back-end data. Views in MVC often need information from various pieces of back-end data. To collect all this information, a separate View Model class is needed. The View Model class is a single front-end class that can communicate with the user interface. It is a simple POJO. It converts the object into JSON output.

Eg: –

 package com.site.common.model;
public class Shop {
String name;
String staffName[];
//getter and setter methods
}

3) Controller: – It takes the following activities.

    1. Jackson library existed on classpath
    2. “mvc:annotation-driven” is enabled
    3. Return method annotated with @ResponseBody

Syntax: –

 public @interface ResponseBody

The above syntax the annotation indicates that a method return value should be bound to the web response body. It also holds annotated handler methods in servlet environment.

The above three steps handle the JSON automatic conversion mechanism.

Eg: –

package com.site.common.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.site.common.model.Shop;
@Controller
@RequestMapping(“/kfc/brands”)
public class JSONController {
@RequestMapping(value=“{name}”, method = RequestMethod.GET)
public @ResponseBody Shop getShopInJSON(@PathVariable String name) {
Shop shop = new Shop();
shop.setName(name);
shop.setStaffName(new String[]{“site1”, “site2”});
return shop;
}
}

4) MVC annotation driven: -For the spring configuration XML file we enable “mvc:annotation-driven”

Eg: –

 <beans xmlns=“http://www.springframework.org/schema/beans”
xmlns:context=“http://www.springframework.org/schema/context”
xmlns:mvc=“http://www.springframework.org/schema/mvc” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=“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.0.xsd”>
<context:component-scan base-package=“com.site.common.controller” />
<mvc:annotation-driven />
</beans>

Example of JSON MVC framework: –

Eg: –

 package com.site.common.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.site.common.model.Shop;
@Controller
@RequestMapping(“/kfc/brands”)
public class JSONController {
@RequestMapping(value=“{name}”, method = RequestMethod.GET)
public @ResponseBody Shop getShopInJSON(@PathVariable String name) {
Shop shop = new Shop();
shop.setName(name);
shop.setStaffName(new String[]{“site1”, “site2”});
return shop;
} }

 

Features of JSON MVC framework

1) Powerful and straightforward configuration of both framework and application classes as JavaBeans. This configuration capability includes easy referencing across contexts, such as from web controllers to business objects and validators.

2) Adaptability, non-intrusiveness, and flexibility. Define any controller method signature you need, possibly using one of the parameter annotations (such as @RequestParam, @RequestHeader, @PathVariable, and more) for a given scenario.

3) Reusable business code, no need for duplication. Use existing business objects as command or form objects instead of mirroring them to extend a particular framework base class.

4) Customizable binding and validation. Type mismatches as application-level validation errors that keep the offending value, localized date and number binding, and so on instead of String-only form objects with manual parsing and conversion to business objects.

5) Customizable handler mapping and view resolution. Handler mapping and view resolution strategies range from simple URL-based configuration, to sophisticated, purpose-built resolution strategies. Spring is more flexible than web MVC frameworks that mandate a particular technique.

6) Flexible model transfer. Model transfer with a name/value Map supports easy integration with any view technology.

7) Customizable locale and theme resolution support for JSPs with or without spring tag library, support for JSTL, support for Velocity without the need for extra bridges, and so on.

8) A simple yet powerful JSP tag library known as the Spring tag library that provides support for features such as data binding and themes. The custom tags allow for maximum flexibility in terms of markup code.

9) A JSP form tag library, introduced in Spring 2.0 that makes writing forms in JSP pages much easier.

10) Beans whose lifecycle is scoped to the current HTTP request or HTTP Session. This is not a specific feature of Spring MVC itself, but rather of the WebApplicationContext container(s) that Spring MVC uses.