HTTP 415錯誤-Unsupported media type詳解
前段時間在使用@RequestBody注解的時候遇到了一個以前沒遇到過的錯誤,HTTP 415 Unsupported media type? 這個是個什么鬼,@ResponseBody可以正常工作而一使用@RequestBody來進行交互就會報這個錯誤。一直請求不到Controller,我開始總以為是路徑或者json格式不對的問題,上網(wǎng)查資料大多也說的是這個問題。可是我已經(jīng)寫了
data : JSON.stringify(user),
dataType : 'json',
contentType : 'application/json;charset=UTF-8',
按照網(wǎng)上的辦法也一直不管用,百思不得其解。于是繼續(xù)在網(wǎng)上找資料,
網(wǎng)上分析原因很多,但找了很久都沒解決,基本是以下幾類:
- springmvc添加配置、注解;
- pom.xml添加jackson包引用;
- Ajax請求時沒有設(shè)置Content-Type為application/json
- 發(fā)送的請求內(nèi)容不要轉(zhuǎn)成JSON對象,直接發(fā)送JSON字符串即可
各種辦法都嘗試了一遍,還是沒有能解決問題;
<script>
jQuery(function($){
var urlStr = "<%=request.getContextPath()%>/user/GetUser";
var user = {
"id" : 6,
"userName" : "小紅",
"password" : "123",
"age" : 12
};
$.ajax({
url : urlStr,
type : "POST",
data : JSON.stringify(user), //轉(zhuǎn)JSON字符串
dataType : 'json',
contentType : 'application/json;charset=UTF-8', //contentType很重要
success : function(result) {
console.log(result);
//alert(result);
//data = eval("(" + result + ")");
//alert(data);
$("#a").html(result.userName);
}
});
});
</script>
造了一個簡單是數(shù)據(jù)來測試,還是不行。。
package com.cn.hnust.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cn.hnust.domain.User;
import com.cn.hnust.service.IUserService;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("/showUser")
public String toIndex(HttpServletRequest request, Model model) {
// int userId = Integer.parseInt(request.getParameter("id"));
// User user = this.userService.getUserById(userId);
// model.addAttribute("user", user);
return "showUser";
}
@RequestMapping(value = "/GetUser", method = RequestMethod.POST)
public @ResponseBody
User GetUser(@RequestBody User user) {
user.setUserName("Wei");
return user;
}
}
控制器也很簡單,可是就是請求不到Controller方法。于是我繼續(xù)在網(wǎng)上尋找資料,直到看到一篇博客,才找到了問題的解決辦法。
原來是Jackson的依賴問題,spring3.x和spring4.x是不同的:
spring3.x是org.springframework.http.converter.json.MappingJacksonHttpMessageConverter
spring4.x是org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
具體可以查看spring-web的jar確認,哪個存在用哪個!
在配置ViewResolver的時候應該指定響應的版本,于是我將springmvc的配置文件改為:
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="order" value="1" /> <property name="mediaTypes"> <map> <entry key="json" value="application/json" /> <entry key="xml" value="application/xml" /> <entry key="htm" value="text/html" /> </map> </property> <property name="defaultViews"> <list> <!-- JSON View --> <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"> </bean> </list> </property> <property name="ignoreAcceptHeader" value="true" /> </bean>
僅僅將
MappingJacksonJsonView
改為
MappingJackson2JsonView
到此這篇關(guān)于HTTP 415錯誤-Unsupported media type詳解的文章就介紹到這了,更多相關(guān)HTTP 415錯誤-Unsupported media type內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot+mybatis+Vue實現(xiàn)前后端分離項目的示例
本文主要介紹了SpringBoot+mybatis+Vue實現(xiàn)前后端分離項目的示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12
Java import導入及訪問控制權(quán)限修飾符原理解析
這篇文章主要介紹了Java import導入及訪問控制權(quán)限修飾符過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-11-11
mybatis對象List<String> List<Integer>屬性映射方式
這篇文章主要介紹了mybatis對象List<String> List<Integer>屬性映射方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
Java數(shù)據(jù)結(jié)構(gòu)之ArrayList從順序表到實現(xiàn)
Java中的ArrayList是一種基于數(shù)組實現(xiàn)的數(shù)據(jù)結(jié)構(gòu),支持動態(tài)擴容和隨機訪問元素,可用于實現(xiàn)順序表等數(shù)據(jù)結(jié)構(gòu)。ArrayList在內(nèi)存中連續(xù)存儲元素,支持快速的隨機訪問和遍歷。通過學習ArrayList的實現(xiàn)原理和使用方法,可以更好地掌握Java中的數(shù)據(jù)結(jié)構(gòu)和算法2023-04-04

