The first time you use Spring MVC, this is also a record in case you forget, I hope experienced friends point out the inadequacy of the place
First, use Maven to manage jars.
[HTML]View Plaincopyprint?
- < Dependency >
- < groupId > Org.codehaus.jackson </ groupId >
- < Artifactid > JACKSON-CORE-LGPL </ Artifactid >
- < version > 1.9.6 </ version >
- </ Dependency >
- < Dependency >
- < groupId > Org.codehaus.jackson </ groupId >
- < Artifactid > JACKSON-CORE-ASL </ Artifactid >
- < version > 1.9.4 </ version >
- </ Dependency >
- < Dependency >
- < groupId > Org.codehaus.jackson </ groupId >
- < Artifactid > JACKSON-MAPPER-ASL </ Artifactid >
- < version > 1.9.5 </ version >
- </ Dependency >
- < Dependency >
- < groupId > Org.codehaus.jackson </ groupId >
- < Artifactid > JACKSON-MAPPER-LGPL </ Artifactid >
- < version > 1.9.6 </ version >
- </ dependency
<dependency><groupid>org.codehaus.jackson</groupid><artifactid>jackson-core-lgpl</ Artifactid><version>1.9.6</version></dependency><dependency><groupid> org.codehaus.jackson</groupid><artifactid>jackson-core-asl</artifactid><version>1.9.4 </version></dependency><dependency><groupId>org.codehaus.jackson</groupId>< artifactid>jackson-mapper-asl</artifactid><version>1.9.5</version></dependency>< dependency><groupid>org.codehaus.jackson</groupid><artifactid>jackson-mapper-lgpl</ Artifactid><version>1.9.6</version></dependency>
Second, the configuration spring-servlet.xml:
[HTML]View Plaincopyprint?
- < Bean class=" Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter ">
- < Property name="messageconverters">
- < List >
- < ref bean="mappingjacksonhttpmessageconverter" />
- </ List >
- </ Property >
- </ Bean >
<bean class= "Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" > < Property name= "Messageconverters" > <list > <ref bean= "Mappingjacksonhttpmessageconverter"/ > </list> </property> </bean>
Mappingjacksonhttpmessageconverter: Used to handle JSON format conversions
[HTML]View Plaincopyprint?
- < Bean id="mappingjacksonhttpmessageconverter" class= "Org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" >
- < Property name="supportedmediatypes">
- < List >
- < value > Application/json; CharSet = UTF -8 </ value >
- </ List >
- </ Property >
- </ Bean >
<bean id= "Mappingjacksonhttpmessageconverter" class= " Org.springframework.http.converter.json.MappingJacksonHttpMessageConverter "> <property name=" Supportedmediatypes "> <list> <value>application/json;charset=UTF-8</value> </list> </property></bean>
1.
If Mappingjacksonhttpmessageconverter can be directly written as follows:
[HTML]View Plaincopyprint?
- < Bean id="mappingjacksonhttpmessageconverter" class= "Org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" / >
<bean id= "Mappingjacksonhttpmessageconverter" class= " Org.springframework.http.converter.json.MappingJacksonHttpMessageConverter "/>
This should be the default:iso-88859-1
2.
Supportedmediatypes can write multiple value in the list
<list>
<value>apolication/json; Charset=utf-8</value>
<value>text/html;charset=UTF-8</value>
</list>
Third, controller configuration @ResponseBody
[Java]View Plaincopyprint?
- @Controller
- Public class Logincontroller {
- @RequestMapping (value="/validatauser.json")
- @ResponseBody
- Public map<string,object> validatauser (@RequestParam String userName) {
- Logger.info ("Validata User: {}", UserName);
- map<string,object> map = new hashmap<string,object> ();
- Map.put ("code", true);
- return map;
- }
- }
@Controllerpublic class Logincontroller {@RequestMapping (value= "/validatauser.json") @ResponseBodypublic map< String,object> Validatauser (@RequestParam String userName) {logger.info ("Validata User: {}", userName); map<string,object> map = new hashmap<string,object> (); Map.put ("code", true); return map;}}
Four: The page still follows the same format as the original Ajax area JSON,
Off Topic:
About @ResponseBody:
Returns the content or object as the HTTP response body, using @responsebody to skip the view processing part, instead calling the appropriate httpmessageconverter and writing the return value to the output stream.
From for notes (Wiz)
Spring MVC returns JSON format-Long-Blog channel-csdn.net