JSON data interaction

Source: Internet
Author: User

1.1.@RequestBody

Role:

@RequestBody annotations are used to read The contents of an HTTP request ( string )by Springmvc provided by Httpmessageconverter The content that the interface will read ( JSON data) is converted to Java object and bind to the Controller the parameters of the method.

Traditional Request Parameters :

Itemedit.action?id=1&name=zhangsan&age=12

Now the request parameter:

Use POST request, adding JSON data to the request body

{"id": 1,"name": "Test Commodity","price": 99.9,"detail": "Test Product description","pic": " 123456.jpg "}

This example applies:

@RequestBody Annotations Implement JSON data that receives HTTP requests , converting the JSON data to Java objects are bound

1.2.@ResponseBody

Role:

@ResponseBody annotations The object that is used to return the Controller 's method through the SPRINGMVC provided by the Httpmessageconverter interface to the specified format of data such as:json,xml , etc., by Response response to the client

This example applies:

The @ResponseBody annotation implementation converts the Controller method return Java object to a JSON response to the client.

1.3. Request JSON, in response to JSON implementations:

1.3.1. Add a jar package

If you need SPRINGMVC support json, you must include the JSON processing jar

we use Jackson This jar, such as:

1.3.2.Itemcontroller Writing

@RequestMapping ("Testjson")//  @ResponseBody public @ResponseBody Item Testjson (@RequestBody item Item) {    return  item;}

1.3.3. Configuring The JSON converter

if without using annotations to drive <mvc:annotation-driven/>, you need to configure a JSON converter for the processor adapter , referencing the custom parameter bindings previously learned.

in the springmvc.xml configuration file, add the JSON converter to the processor adapter:

<!--processor adapter--    class= " Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter ">        <property name=" Messageconverters ">        <list>        class=" Org.springframework.http.converter.json.MappingJacksonHttpMessageConverter "></bean>        </list >        </property>    </bean>

JSON data interaction

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.