Spring MVC returns JSON format-Long-Blog channel-csdn.net

Source: Internet
Author: User

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?
  1. < Dependency >   
  2. < groupId > Org.codehaus.jackson </ groupId >   
  3. < Artifactid > JACKSON-CORE-LGPL </ Artifactid >   
  4. < version > 1.9.6 </ version >   
  5. </ Dependency >   
  6. < Dependency >   
  7. < groupId > Org.codehaus.jackson </ groupId >   
  8. < Artifactid > JACKSON-CORE-ASL </ Artifactid >   
  9. < version > 1.9.4 </ version >   
  10. </ Dependency >   
  11. < Dependency >   
  12. < groupId > Org.codehaus.jackson </ groupId >   
  13. < Artifactid > JACKSON-MAPPER-ASL </ Artifactid >   
  14. < version > 1.9.5 </ version >   
  15. </ Dependency >   
  16. < Dependency >   
  17. < groupId > Org.codehaus.jackson </ groupId >   
  18. < Artifactid > JACKSON-MAPPER-LGPL </ Artifactid >   
  19. < version > 1.9.6 </ version >   
  20. </ 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?
  1. < Bean class=" Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter ">
  2. < Property name="messageconverters">
  3. < List >
  4. < ref bean="mappingjacksonhttpmessageconverter" />
  5. </ List >       
  6. </ Property >       
  7. </ 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?
  1. < Bean id="mappingjacksonhttpmessageconverter" class= "Org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" >     
  2. < Property name="supportedmediatypes">
  3. < List >     
  4. < value > Application/json; CharSet = UTF -8 </ value >     
  5. </ List >     
  6. </ Property >   
  7. </ 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?
    1. < 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?
  1. @Controller   
  2. Public class Logincontroller {
  3. @RequestMapping (value="/validatauser.json")
  4. @ResponseBody   
  5. Public map<string,object> validatauser (@RequestParam String userName) {
  6. Logger.info ("Validata User: {}", UserName);
  7. map<string,object> map = new hashmap<string,object> ();
  8. Map.put ("code", true);
  9. return  map;
  10. }
  11. }
@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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.