解決@RequestBody接收json對象報錯415的問題
@RequestBody接收json對象報錯415
前端請求:
$.ajax({ url: basePath() + "/index/login.do", type : "post", data: JSON.stringify(form), dataType : "json", contentType : "application/json;charset=utf8", success: function (data) { console.log(data); }, error: function () { } });
后端接收:
@ResponseBody @RequestMapping(value = "/login",method = RequestMethod.POST,produces = "application/json;charset=utf8") public JSONObject login(@RequestBody LoginVo loginVo){ JSONObject result = new JSONObject(); UsernamePasswordToken token = new UsernamePasswordToken(loginVo.getUsername(),loginVo.getPassword()); System.out.println(loginVo.isRememberMe()); Subject subject = SecurityUtils.getSubject(); subject.login(token); if (subject.isAuthenticated()){ result.put("result",true); }else{ result.put("result",false); } return result; }
前端ajax請求,后端使用@RequestBody接收,報出415請求數(shù)據(jù)格式錯誤
錯誤原因:
springMVC無法讀取ajax設(shè)置好的dataType并以對應的方式處理請求頭,進而無法處理json數(shù)據(jù)
解決辦法:
在maven中引入Jackson相關(guān)jar包,并在springMVC的xml中引入相關(guān)配置,maven和springMVC的相關(guān)代碼如下:
maven:
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.6</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.6</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.6</version> </dependency>
springMVC:
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" /> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <!-- 設(shè)置返回字符串編碼 --> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name = "supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>application/json;charset=UTF-8</value> </list> </property> </bean> <!-- json轉(zhuǎn)換器 --> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </list> </property> </bean>
后端使用@RequestBody接收前端傳來的數(shù)據(jù)
踩坑①
@RequestBody接收json字符串,只能使用post的提交方式
前端直接復制了相似功能頁面的js,該頁面是使用的get的提交方式
但前端報錯500,后端報錯提示
2019-09-12 09:17:43.088 ERROR GlobalExceptionHandler : An exception occurs within the system : Required String parameter ‘xxx' is not present
踩坑②
后將.get(URL,data,callback)修改為.post(URL,data,callback);
$.post(URL,data,callback);
必需的 URL 參數(shù)規(guī)定您希望請求的 URL。
可選的 data 參數(shù)規(guī)定連同請求發(fā)送的數(shù)據(jù)。
可選的 callback 參數(shù)是請求成功后所執(zhí)行的函數(shù)名
但前端繼續(xù)報錯500,后端報錯提示
2019-09-12 09:23:15.409 ERROR GlobalExceptionHandler : An exception occurs within the system : Content type ‘a(chǎn)pplication/x-www-form-urlencoded;charset=UTF-8' not supported
踩坑③
后端提示不支持Content type 為'application/x-www-form-urlencoded;charset=UTF-8'的格式,百度查了一下.post(URL,data,callback)只是預配置.ajax調(diào)用的快捷方式,并不能修改contentType的類型
所以將$.post方法修改為了&.ajax方法
設(shè)置
type: “post”, url: ctx + url, data: JSON.stringify(allData), dataType: “json”, contentType:“application/json;charset=utf-8”,
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Mybatis批量插入數(shù)據(jù)返回主鍵的實現(xiàn)
這篇文章主要介紹了Mybatis批量插入數(shù)據(jù)返回主鍵的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-01-01基于Zookeeper實現(xiàn)服務注冊和服務發(fā)現(xiàn)功能
無論是采用SOA還是微服務架構(gòu),都需要使用服務注冊和服務發(fā)現(xiàn)組件,本文將基于 Zookeeper 實現(xiàn)服務注冊和服務發(fā)現(xiàn)功能,如果跟我一樣有同樣的困惑,希望可以通過本文了解其他組件如何使用 Zookeeper 作為注冊中心的工作原理2023-09-09使用Spring Boot創(chuàng)建Web應用程序的示例代碼
本篇文章主要介紹了使用Spring Boot創(chuàng)建Web應用程序的示例代碼,我們將使用Spring Boot構(gòu)建一個簡單的Web應用程序,并為其添加一些有用的服務,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05JAVA 文件監(jiān)控 WatchService的示例方法
本篇文章主要介紹了JAVA 文件監(jiān)控 WatchService的示例方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10javaWeb連接數(shù)據(jù)庫實現(xiàn)簡單登陸注冊功能的全過程
初學javaWeb,老師留下一小作業(yè),用JAVA實現(xiàn)與服務器端交互,實現(xiàn)登錄和注冊功能,下面這篇文章主要給大家介紹了關(guān)于javaWeb連接數(shù)據(jù)庫實現(xiàn)簡單登陸注冊功能的相關(guān)資料,需要的朋友可以參考下2022-06-06java?for循環(huán)內(nèi)執(zhí)行多線程問題
這篇文章主要介紹了java?for循環(huán)內(nèi)執(zhí)行多線程問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03IDEA導入外部項目報Error:java: 無效的目標發(fā)行版: 11的解決方法
這篇文章主要介紹了IDEA導入外部項目報Error:java: 無效的目標發(fā)行版: 11,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09