首页 > 编程技术 > java

HTTP 415错误-Unsupported media type详解

发布时间:2021-8-16 16:00

   前段时间在使用@RequestBody注解的时候遇到了一个以前没遇到过的错误,HTTP 415  Unsupported media type? 这个是个什么鬼,@ResponseBody可以正常工作而一使用@RequestBody来进行交互就会报这个错误。一直请求不到Controller,我开始总以为是路径或者json格式不对的问题,上网查资料大多也说的是这个问题。可是我已经写了

data : JSON.stringify(user),  

dataType : 'json',

contentType : 'application/json;charset=UTF-8',

按照网上的办法也一直不管用,百思不得其解。于是继续在网上找资料,

网上分析原因很多,但找了很久都没解决,基本是以下几类:

各种办法都尝试了一遍,还是没有能解决问题;

<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), //转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>

造了一个简单是数据来测试,还是不行。。

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方法。于是我继续在网上寻找资料,直到看到一篇博客,才找到了问题的解决办法。

原来是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

到此这篇关于HTTP 415错误-Unsupported media type详解的文章就介绍到这了,更多相关HTTP 415错误-Unsupported media type内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!

标签:[!--infotagslink--]

您可能感兴趣的文章: