SPRINGMVC Study Notes (-json) data interaction
- SPRINGMVC Learning notes 18-json data interaction
- SPRINGMVC for JSON interaction
- Environment preparation
- Adding dependencies for JSON transformations
- Configuring the JSON Converter
- JSON Interactive test
- Input JSON string output is JSON string
- Input keyvalue output is JSON string
This article mainly describes how to interact with JSON data in SPRINGMVC, first environment preparation and configuration, then the "input JSON string, output is JSON string" and "input key/value, output is JSON string" Two cases of interaction
SPRINGMVC for JSON interaction
JSON data format in the interface calls, HTML pages are more commonly used, the JSON format is relatively simple, parsing is more convenient.
For example: WebService interface, transfer JSON data.
- Request JSON, output JSON, request is the JSON string, so in front-end pages need to convert the contents of the request to JSON, not very convenient.
- Request Key/value, output JSON. This method is more commonly used.
Environment ready to add dependencies for JSON transformations
At first I was less jackson-databind dependent, the program various error.
<!--JSON conversion --<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>
Viewing a 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.: compile[info] \-org. Codehaus. Jackson: Jackson-core-asl:jar:1.9.: Compile
Configuring the JSON Converter
Adding in the annotation adaptermessageConverters
<!--Note 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 /> you use it, you do not define the contents above.
JSON Interactive test
Show two buttons to test separately
<%--Created by IntelliJ idea. User:brian Date: 3/7time: $ Change this template use File | Settings | File templates.--%> <%@ page contenttype="Text/html;charset=utf-8" language="java" %><html><head> <meta http-equiv="Content-type" Content="text/html; Charset=utf-8 "> <title>JSON Interactive test</title> <script type="text/javascript" src="${ PageContext.request.contextPath}/js/jquery-1.4.4.min.js "></script> <script type="Text/javascript"> //Request JSON, output is JSONfunction requestjson(){ omitted} //Request key/v Alue, output is JSONfunction responsejson(){ omitted} </script></head><body><input type="button" onclick="Requestjson ()" value= "Request JSON, output is json"/><input type="button" onclick="Responsejson ()" Value="Request Key/value, Output is json"/></body>
@Controllerpublicclass JsonTest { 省略}
Input JSON string, output is JSON string
Use jquery's AJAX submission JSON string to parse the output JSON results.
//request JSON, output is JSON function Requestjson () { $.ajax ({type: ' post ' , Url: ' ${pagecontext.request.contextpath}/requestjson.action ' , ContentT Ype: ' application/json;charset=utf-8 ' , //data format is a JSON string, Product information data: ' {" name phone ", " price": 999} ' , Success:function {//return json result alert (data); } });}
//请求json串(商品信息),输出json(商品信息)//@RequestBody将请求的商品信息的json串转成itemsCustom对象//@ResponseBody将itemsCustom转成json输出@RequestMapping("/requestJson")publicrequestJson(@RequestBody ItemsCustom itemsCustom){ //@ResponseBody将itemsCustom转成json输出 return itemsCustom;}
As you can see, the HTTP headers of request and response are Content-typeapplication/json;charset=utf-8
Input key/value, output is JSON string
Using jquery's Ajax to commit key/value strings, parsing the output JSON results
//request Key/value, The output is JSON function Responsejson () { $.ajax ({type: ' post ' , Url: ' ${pagecontext.request.contextpath}/responsejson.action ' , //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, product info dat A: name= mobile &price=999 ' , Success:function { // Returns the JSON result alert (data.name); } });}
//请求key/value,输出json@RequestMapping("/responseJson")publicresponseJson(ItemsCustom itemsCustom){ //@ResponseBody将itemsCustom转成json输出 return itemsCustom;}
As you can see, the default content-type for Key/value key-value pairs is application/x-www-form-urlencoded that, at the same time, we receive the response "phone"
Author @brianway More articles: personal website | CSDN | Oschina
SPRINGMVC Study Notes (-json) data interaction