Json (JavaScript Object Notation), it is a lightweight data interchange format, simple format, easy to read and write, is currently used in a particularly wide range. So this blog we mainly talk about in the SPRINGMVC, how to parse and transform the JSON data format?
This blog source link: http://pan.baidu.com/s/1kURnwDx Password: b37t
1. Two modes of interaction
Shows the two formats for requesting data from a client, one that requests JSON data directly, and the other is Key/value data. But no matter what kind of data the request is, it is convenient to parse the result on the front page. Eventually we all convert to JSON data format.
2, import the corresponding jar package (see source for details)
3. Configure the JSON converter in the Springmvc.xml file
The first method:
<mvc:annotation-driven ></mvc:annotation-driven>
The second method:
<!--used to convert an object to JSON-- <bean id= "Stringconverter" class= " Org.springframework.http.converter.StringHttpMessageConverter "> <property name=" Supportedmediatypes " > <list> <value>text/plain;charset=UTF-8</value> </list> </ property> </bean> <bean id= "Jsonconverter" class= " Org.springframework.http.converter.json.MappingJackson2HttpMessageConverter "></bean> <bean class= "Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" > < Property name= "Messageconverters" > <list> <ref bean= "Stringconverter"/> <ref bean= "Jsonconverter"/> </list> </property> </bean>
4. Request for JSON data test
Here we need to take note of two annotations:
@ResponseBody convert the background Pojo to a JSON object and return to the page.
@RequestBody accept the foreground JSON data and automatically encapsulate the JSON data Pojo.
①, JSP page
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
②, Controller
Package Com.ys.controller;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestbody;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.responsebody;import Com.ys.po.User; @Controllerpublic class Usercontroller {//Request for JSON, return json@requestmapping ("/requestjson")//@RequestBody convert the JSON string of the request information to the user object//@ Responsebody convert the User object to JSON output @responsebodypublic user Requestjson (@RequestBody user user) throws exception{ SYSTEM.OUT.PRINTLN (user); return user;//return the user to JSON format due to @responsebody annotations}}
③, testing
We visit the JSP page above and click on the button to enter the Controller
Then we look at the returned data:
5. Request for Key/value Data test①, JSP page
②, Controller
The request is Key/value, which returns json@requestmapping ("/requestkeyvalue")//@RequestBody The JSON string that requests the information to the user object @responsebodypublic User requestkeyvalue (user user) throws exception{system.out.println (user); return user;}
③, testing
Then return the data:
6. Problems encountered ①, the following code, because we use Ajax submissions, we introduced a jquery file on the JSP page, found that no matter the absolute path or relative path, the system will always find this file?
<script type= "Text/javascript" src= "${pagecontext.request.contextpath}/js/jquery-2.1.4.min.js" ></script >
Cause: Because you are in the Web. xml file, the configuration for the filter is to intercept all requests. So a file similar to *.js, or *.CSS, is intercepted, so it can't be accessed.
Workaround:
The first approach: we can use the interceptor configured above to intercept only *.do, or *.action, instead of "/". Then the SPRINGMVC container will not intercept files such as *.JS,*.CSS. But this style does not support Restful, it is recommended not to adopt.
Second method: Configure the Interceptor's filtering request in Web. xml
<!--to write in front of the Dispatcherservlet, let Defaultservlet first intercept the request, so that the request will not enter spring, I think performance is the best. - <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern >*.css</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name >default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping>
Third approach: Configure non-filtering of static resources in Spingmvc.xml
<!--Configuring a static file filter--><mvc:resources location= "/web-inf/css/" mapping= "/css/**"/><mvc:resources location = "/web-inf/js/" mapping= "/js/**"/>
②, is also more prone to make mistakes 415
There are many reasons for this error. We need a step-by-Step troubleshooting:
The first step: you must ensure that the imported Jackson-related jar packages are full.
The second step: the configuration of the JSON converter in the Springmvc.xml file must not be missing, how to view this blog's 3rd
Step three: When writing an AJAX request. ContentType: "Application/json;charset=utf-8", do not write ContentType this attribute
Fourth step: Ajax to the background do not pass the string directly, to convert to JSON, that is data:JSON.stringify (Datajson),
Springmvc detailed (vi)------interacting with JSON