Objective
SPRINGMVC is one of the current mainstream web MVC frameworks.
If a classmate is not familiar with it, then please refer to its introductory blog:http://www.cnblogs.com/fangjian0423/p/springmvc-introduction.html
Phenomenon
The demo used in this article is based on Maven and continues to be written based on the example of an entry-level blog.
Let's look at the corresponding phenomenon first. The key configuration in our profile *-dispatcher.xml is as follows (other regular configuration files are not explained, refer to the entry blog mentioned in the beginning of this article):
(View configuration omitted)
<mvc:resources location= "/static/" mapping= "/static/**"/>
<mvc:annotation-driven/>
< Context:component-scan base-package= "Org.format.demo.controller"/>
The following dependencies are required in the POM (Spring dependencies and other dependencies are not displayed):
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId> jackson-core-asl</artifactid>
<version>1.9.13</version>
</dependency>
< dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId> jackson-mapper-asl</artifactid>
<version>1.9.13</version>
</dependency>
This dependency is the dependency of JSON serialization.
Ok. We add a method to the controller:
@RequestMapping ("/xmlorjson")
@ResponseBody public
map<string, object> Xmlorjson () {
map< String, object> map = new hashmap<string, object> ();
Map.put ("List", Employeeservice.list ());
return map;
}
Direct Access Address:
We see a few lines of configuration. After using the @responsebody annotation, the object returned by controller is automatically converted to the corresponding JSON data, where it has to lament the power of SPRINGMVC.
We do not seem to see the specific configuration, the only thing to see is *-dispatcher.xml in a configuration:<mvc:annotation-driven/>. This configuration, in fact, causes the Java object to automatically convert to a JSON object.
So how does spring actually implement the automatic conversion of Java objects to JSON objects? Why convert to JSON data and what if you want to convert it to XML data?
SOURCE Analysis
The spring version used in this article is 4.0.2.
Before we explain <mvc:annotation-driven/> this configuration, let's take a look at the message conversion mechanism for the next spring. @ResponseBody This annotation is the use of the message conversion mechanism, which is eventually converted to JSON data via a JSON converter.
The Httpmessageconverter interface is the HTTP message translation interface that spring provides. For this knowledge, you can refer to the second link in "Resources", which is very clear.