Springmvc three ways to return JSON data

Source: Internet
Author: User
Tags tojson

1, the first way is the product of the spring2 era, that is, each JSON view controller configuration of a jsoniew.

such as: <bean id= "Defaultjsonview" class= "Org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>

or <bean id= "Defaultjsonview" class= "Org.springframework.web.servlet.view.json.MappingJackson2JsonView"/>

Also use Jackson's jar package.

2. The second uses the JSON tool to serialize objects into JSON, a common tool jackson,fastjson,gson.

Take advantage of HttpServletResponse, then get Response.getoutputstream () or Response.getwriter ()

Direct output.

Example:

[Java]View Plaincopyprint?
  1. Public class Jsonutil
  2. {
  3. private static Gson gson=new Gson ();
  4. /** 
  5. * @MethodName: ToJson
  6. * @Description: Convert object to JSON string, this method can meet most requirements
  7. * @param src
  8. *: The object that will be converted
  9. * @return: Converted JSON string
  10. */
  11. public static String ToJson (Object src) {
  12. if (src = = null) {
  13. return Gson.tojson (jsonnull.instance);
  14. }
  15. return Gson.tojson (SRC);
  16. }
  17. }


3, the third use of Spring MVC3 annotation @responsebody

For example:

[Java]View Plaincopyprint?
  1. @ResponseBody
  2. @RequestMapping ("/list")
  3. Public list<string> List (Modelmap modelmap) {
  4. String hql = "Select C from clothing C";
  5. page<clothing> page = new page<clothing> ();
  6. Page.setpagesize (6);
  7. page = clothingserviceimpl.queryforpagebyhql (page, HQL);
  8. return Page.getresult ();
  9. }

You can then return the JSON using the spring MVC default configuration, but you'll need Jackson's jar package.

Note: When using <mvc:annotation-driven/> in Springmvc-servlet.xml, If it is 3.1 before the default injection annotationmethodhandleradapter,3.1 after the default injection requestmappinghandleradapter just add the above mentioned jar package!

If it is manual injection requestmappinghandleradapter can be set

The configuration is as follows:

[HTML]View Plaincopyprint?
  1. <Bean class="Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"
  2. p:ignoredefaultmodelonredirect="true" >
  3. <property name="Messageconverters">
  4. <list>
  5. <Bean class="Org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
  6. </list>
  7. </Property>
  8. </Bean>

Add a Package
Jackson-mapper-asl-*.jar
Jackson-core-asl-*.jar

It can be seen that the use of more and more simple, programmers more and more silly, do not know is good, or bad things ...

Springmvc three ways to return JSON data

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.