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