SpringBoot前后端json數(shù)據(jù)交互的全過程記錄
一、參考文獻(xiàn)
SpringMVC接受JSON參數(shù)詳解及常見錯誤總結(jié)
- 提交方式為 POST 時,
- JQuery Ajax 以 application/x-www-form-urlencoded 上傳 JSON對象 ,
- 后端用 @RequestParam 或者Servlet 獲取參數(shù)。
- JQuery Ajax 以 application/json 上傳 JSON字符串,
- 后端用 @RquestBody 獲取參數(shù)。
- 總結(jié)成表
Controller接收參數(shù)以及參數(shù)校驗(yàn)
AJAX POST請求中參數(shù)以form data和request payload形式在servlet中的獲取方式
二、勇敢嘗試
前端js發(fā)送ajax請求( application/x-www-form-urlencoded )
var jsonObj = {"openid":"xxx","username":"Ed sheeran","password":"123"}; /* Jquery默認(rèn)Content-Type為application/x-www-form-urlencoded類型 */ $.ajax({ type: 'POST', url: "/login", dataType: "json", data: JSON.stringify(jsonObj), success: function(data) { console.log(data) }, error: function() { console.log("fucking error") } });
后端Servlet接受參數(shù)。前端報 200,后端報 返回值都是null
@Controller public class LoginController { @PostMapping("/login") public void login(HttpServletRequest request){ System.err.println(request.getParameter("openid")); System.err.println(request.getParameter("username")); System.err.println(request.getParameter("password")); }
后端改 @RequestParam 接受參數(shù)。前端報 404,后端報 Required String parameter ‘username’ is not present
@Controller public class LoginController { @PostMapping("/login") public void login(@RequestParam("username") String username, @RequestParam("password") String password, @RequestParam("openid") String openid){ System.err.println(username); System.err.println(password); System.err.println(openid); }
后端改 @RequestBody 接受參數(shù)。前端報 415,后端報 Content type ‘application/x-www-form-urlencoded;charset=UTF-8’ not supported
Http status 415 Unsupported Media Type
What is 415 ?
HTTP 415 Unsupported Media Type
The client error response code indicates that the server refuses to accept the request because the payload format is in an unsupported format.
Let’s look at the broswer ?
How to resolve 405 problem when using ajax post @ResponseBody return json data ?
- if you use Spring 4.x jar,please following me(Maven Repository-Spring 4.x.jar)
- add related jar package
spring-webmvc.x.x.jar
jackson-databind.jar
jackson-core.jar
jackson-annotations.jar
- In Spring Configuration file,add following code
<bean class="org.springframework.web.servlet.mvc. annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="jsonHttpMessageConverter" /> </list> </property> </bean> <bean id="jsonHttpMessageConverter" class="org.springframework.http.converter.json. MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean>
- In ajax , set contentType : ‘application/json;charse=UTF-8’
var data = {"name":"jsutin","age":18}; $.ajax({ url : "/ASW/login.html", type : "POST", data : JSON.stringify(data), dataType: 'json', contentType:'application/json;charset=UTF-8', success : function(result) { console.log(result); } });In server ,using @RequestBody anotation receive json data,and using @ResponseBody anotation response json to jsp page
@RequestMapping(value = "/login",method = RequestMethod.POST) public @ResponseBody User login(@RequestBody User user){ system.out.println(user.getName); system.out.println(user.getAge); }
@Controller public class LoginController { @PostMapping("/login") public void login(@RequestBody Map<String,Object> map){ System.err.println(map.get("username")); System.err.println(map.get("password")); System.err.println(map.get("openid")); }
前端加 contentType : “application/json”。前端報 200,后端 能接受到參數(shù)
$.ajax({ type: 'POST', url: "/login", dataType: "json", data: JSON.stringify(jsonObj), contentType : "application/json", success: function(data) { console.log(data) }, error: function() { console.log("fucking error") } });
@Controller public class LoginController { @PostMapping("/login") public void login(@RequestBody Map<String,Object> map){ System.err.println(map.get("username")); System.err.println(map.get("password")); System.err.println(map.get("openid")); } }
有時候,我想在后端使用對象來獲取參數(shù)。前端報 200,后端 也ok
@Controller public class LoginController { @PostMapping("/login") public void login(@RequestBody Form form){ System.err.println(form); } }
public class Form { private String openid; private String username; private String password; public String getOpenid() { return openid; } public void setOpenid(String openid) { this.openid = openid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "Form{" + "openid='" + openid + '\'' + ", username='" + username + '\'' + ", password='" + password + '\'' + '}'; } }
三、最終選擇交互方式
前端 application/json,上傳 josn字符串, 后端 使用對象 或者 Map
前端代碼
var jsonObj = {"openid":"xxx","username":"Ed sheeran","password":"123"}; /* Jquery默認(rèn)Content-Type為application/x-www-form-urlencoded類型 */ $.ajax({ type: 'POST', url: "/login", dataType: "json", data: JSON.stringify(jsonObj), contentType : "application/json", success: function(data) { console.log(data) }, error: function() { console.log("fucking error") } });
后端代碼1
@Controller public class LoginController { @PostMapping("/login") public void login(@RequestBody Form form){ System.err.println(form); } }
后端代碼2
@Controller public class LoginController { @PostMapping("/login") public void login(@RequestBody Map<String,Object> map){ System.err.println(map.get("username")); System.err.println(map.get("password")); System.err.println(map.get("openid")); } }
總結(jié)
到此這篇關(guān)于SpringBoot前后端json數(shù)據(jù)交互的文章就介紹到這了,更多相關(guān)SpringBoot前后端json數(shù)據(jù)交互內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot如何返回Json數(shù)據(jù)格式
- Spring/SpringBoot?@RequestParam注解無法讀取application/json格式數(shù)據(jù)問題解決
- Springboot?中的?Filter?實(shí)現(xiàn)超大響應(yīng)?JSON?數(shù)據(jù)壓縮的方法
- SpringBoot實(shí)現(xiàn)前后端、json數(shù)據(jù)交互以及Controller接收參數(shù)的幾種常用方式
- SpringBoot響應(yīng)Json數(shù)據(jù)亂碼通過配置的解決
- springboot 返回json格式數(shù)據(jù)時間格式配置方式
- 從前端Vue到后端Spring Boot接收J(rèn)SON數(shù)據(jù)的正確姿勢(常見錯誤及問題)
相關(guān)文章
java的三種隨機(jī)數(shù)生成方式的實(shí)現(xiàn)方法
這篇文章主要介紹了java的三種隨機(jī)數(shù)生成方式的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09Java8中Optional類型和Kotlin中可空類型的使用對比
這篇文章主要給大家介紹了關(guān)于Java8中Optional類型和Kotlin中可空類型的使用對比,文中通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09Java程序開發(fā)中abstract 和 interface的區(qū)別詳解
abstract class和interface在Java語言中都是用來進(jìn)行抽象類。但是兩者有什么區(qū)別呢,接下來小編給大家?guī)砹薬bstract 和 interface的區(qū)別詳解,感興趣的朋友一起學(xué)習(xí)吧2016-06-06mybatis自定義參數(shù)類型轉(zhuǎn)換器數(shù)據(jù)庫字段加密脫敏
這篇文章主要為大家介紹了mybatis自定義參數(shù)類型轉(zhuǎn)換器數(shù)據(jù)庫字段加密脫敏,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09