Springmvc learning notes (18)-json Data Interaction
Springmvc learning notes (18)-json Data Interaction
This article mainly introduces how to interact json data in springmvc. Firstly, we prepare and configure the environment, and then demonstrate "input json string, output is json string" and "Input key/value, the output is a json string.
Springmvc performs json Interaction
Json data format is commonly used in interface calls and html pages. json format is simple and easy to parse.
For example, the webservice interface transmits json data.
The request json and output json must be a json string. Therefore, it is not convenient to convert the request content to json on the front-end page. Request key/value and output json. This method is commonly used. Environment ready to add dependencies for json Conversion
At first I lostjackson-databindVarious program errors.
Com. fasterxml. jackson. core
Jackson-databind
2.7.2
Org. codehaus. jackson
Jackson-mapper-asl
1.9.13
View dependency tree
[INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.7.2:compile[INFO] | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.7.0:compile[INFO] | \- com.fasterxml.jackson.core:jackson-core:jar:2.7.2:compile[INFO] \- org.codehaus.jackson:jackson-mapper-asl:jar:1.9.13:compile[INFO] \- org.codehaus.jackson:jackson-core-asl:jar:1.9.13:compile
Configure the json Converter
Add to annotation AdaptermessageConverters
Note: If you use You do not need to define the above content.
Json interaction Test
Show two buttons to test separately
Jsp page
<%-- Created by IntelliJ IDEA. User: brian Date: 2016/3/7 Time: 20:49 To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %>
<Script type = "text/javascript" src = "$ {pageContext. request. contextPath}/js/jquery-1.4.4.min.js "> </script> <script type =" text/javascript "> // request json, the output is json function requestJson () {omitted} // request key/value, and the output is json function responseJson () {omitted} </script>
Controller
@ Controllerpublic class JsonTest {omitted}
The test result is a json string. The output is a json string.
Use ajax of jquery to submit a json string and parse the output json result.
Jsp page
// Request json. The output is jsonfunction requestJson () {$. ajax ({type: 'post', url: '$ {pageContext. request. contextPath}/requestJson. action ', contentType: 'application/json; charset = UTF-8', // the data format is a json string, and the product information data: '{"name": "Mobile Phone ", "price": 999} ', success: function (data) {// return the json result alert (data );}});}
Controller
// Request json string (product information) and output json (Product Information) [email protected] convert the json string of the requested product information into itemsCustom object [email protected] and convert itemsCustom to json output @ RequestMapping ("/requestJson ") public @ ResponseBody ItemsCustom requestJson (@ RequestBody ItemsCustom itemsCustom) {[email protected] convert itemsCustom to json and return itemsCustom ;}
Test Results
We can see that the Content-Type of the request and response HTTP headers is bothapplication/json;charset=utf-8
Input key/value, and output is a json string
Use ajax of jquery to submit the key/value string and parse the output json result
Jsp page
// Request key/value. The output is jsonfunction responseJson () {$. ajax ({type: 'post', url: '$ {pageContext. request. contextPath}/responseJson. action ', // The request is key/value. You do not need to specify the contentType here, because the default value is key/value type // contentType: 'application/json; charset = UTF-8 ', // The data format is a json string. The product information data: 'name = Mobile Phone & price = 000000', success: function (data) {// return the json result alert (data. name );}});}
Controller
// Request key/value, output json @ RequestMapping ("/responseJson") public @ ResponseBody jsonresponsejson (ItemsCustom) {[email protected] convert itemsCustom to json output return itemsCustom ;}
Test Results
We can see that the default Content-Type of key-value pairs isapplication/x-www-form-urlencodedAt the same time, we received the response "Mobile Phone"