Spring MVC for json interaction
json data format is commonly used in interface calls and html pages. json format is simple and easy to parse.
- Request json, output json. It is not convenient for the front page to convert the requested content into json.
- Request key/value and output json. This method is commonly used.
Environmental preparation
Add dependency for json transformation
<!-- json Transformation--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.7.2</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency>
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 messageConverters to annotation adapter
<!--Annotation adapter --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean> </list> </property> </bean>
Note: if < MVC: annotation driven / > is used, the content above is not defined.
Input json string, output json string
Use jquery's ajax to submit json string and parse the output json results.
- jsp page
//Request json, the output is json function requestJson(){ $.ajax({ type:'post', url:'${pageContext.request.contextPath }/requestJson.action', contentType:'application/json;charset=utf-8', //Data format is json string, commodity information data:'{"name":"Mobile phone","price":999}', //Return json result success:function(data){ alert(data); } }); }
- controller
@RequestMapping("/requestJson") public @ResponseBody ItemsCustom requestJson(@RequestBody ItemsCustom itemsCustom){ return itemsCustom; }
- @RequestBody converts the json string of the requested product information into an itemsCustom object
- @ResponseBody converts itemsCustom to json output
Enter key/value and the output is json string
Using jquery's ajax to submit the key/value string to parse the output json results
- jsp page
//Request key/value, the output is json function responseJson(){ $.ajax({ type:'post', url:'${pageContext.request.contextPath }/responseJson.action', //The request is key/value. There is no need to specify contentType, because the default is key/value type //contentType:'application/json;charset=utf-8', //Data format is json string, commodity information data:'name=Mobile phone&price=999', //Return json result success:function(data){ alert(data.name); } }); }
- controller
@RequestMapping("/responseJson") public @ResponseBody ItemsCustom responseJson(ItemsCustom itemsCustom){ return itemsCustom; }