欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

org.springframework.web.HttpMediaTypeNotSupportedException: Unsupported Media Type, status=415

程序员文章站 2022-07-12 20:28:54
...

org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'null' not supported


Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Thu Jan 04 17:33:41 CST 2018

There was an unexpected error (type=Unsupported Media Type, status=415).

Content type 'null' not supported


产生错误的代码:

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
@Scope("prototype")
@RequestMapping("/rest/test")
public class TestController {

	@Autowired
	private BaseAreaMapper baseAreaMapper;
	
	public TestController() {
		// TODO Auto-generated constructor stub
	}

	@RequestMapping(value = "/users/{username}", method = RequestMethod.GET, consumes = "application/json")
	public @ResponseBody String getUser(@PathVariable(value="username") String username, @RequestParam(value="pwd", required=false) String pwd) throws Exception  {
		return "Welcome," + username + ",Your pwd is:" + pwd ;
	}
	
}
其实问题的关键就是:

@RequestMapping(value = "/users/{username}", method = RequestMethod.GET, consumes = "application/json")
中的“
, consumes = "application/json"

把这个参数去掉就可以了。访问结果如下:

org.springframework.web.HttpMediaTypeNotSupportedException: Unsupported Media Type, status=415

 上面去掉的参数什么意思呢?

其实是要求客户端调用的时候,以 

application/json
格式提交参数数据。

参考: 

java.io.IOException: Server returned HTTP response code: 415 for URL:xxxxxx

另见下面的 AJAX 调用示例:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>测试 type=Unsupported Media Type, status=415 问题</title>
<meta name="keywords" content="Unsupported,Media,Type,status,415" />
<meta name="description"
	content="测试 type=Unsupported Media Type, status=415 问题" />

<script type="text/javascript"
	src="jquery-1.10.2.min.js"></script>
<script type="text/javascript">
	function doRequest() {
		$.ajax({
			url : "/rest/test/users/abc",
			contentType:"application/json",
			context : document.body,
			data:{"pwd":"123"},
			dataType:"text",
			success : function(data) {
				$("#txtRep").val(data);
			},
			error : function(req, err, ex) {
				$("#txtRep").val(req.responseText + "\r\n" + err + "\r\n" + ex);
			}
		});
	}
</script>
</head>

<body>
	<p>测试 type=Unsupported Media Type, status=415 问题</p>

	<textarea id="txtRep" cols="50" rows="20"></textarea>
	<input type="button" value="submit" onclick="doRequest()">

</body>
</html>
指定:
contentType:"application/json"

org.springframework.web.HttpMediaTypeNotSupportedException: Unsupported Media Type, status=415