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

简介: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'null' not supportedp.p1 {margin: 0.

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"

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


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

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

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"



目录
相关文章
|
7月前
|
Java 应用服务中间件 Maven
Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerExcepti
Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerExcepti
179 0
|
8天前
|
Java
org.springframework.web.util.NestedServletException: Request processing failed; nested exception....
org.springframework.web.util.NestedServletException: Request processing failed; nested exception....
39 0
|
6月前
|
SQL 数据库
org.flywaydb.core.api.FlywayException: Schema “xxx” contains a failed migration to version 156!
org.flywaydb.core.api.FlywayException: Schema “xxx” contains a failed migration to version 156!
|
11月前
|
Java 应用服务中间件 Maven
Error configuring application listener of class org.springframework.web.context.ContextLoaderListene
Error configuring application listener of class org.springframework.web.context.ContextLoaderListene
83 0
|
关系型数据库 MySQL 数据库
Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
214 0
|
前端开发 Java 应用服务中间件
Error configuring application listener of class org.springframework.web.context.ContextLoader
Error configuring application listener of class org.springframework.web.context.ContextLoader
Error configuring application listener of class org.springframework.web.context.ContextLoader
|
Java Spring
required a bean of type ‘org.springframework.web.client.RestTemplate‘ that could not be found.
required a bean of type ‘org.springframework.web.client.RestTemplate‘ that could not be found.
required a bean of type ‘org.springframework.web.client.RestTemplate‘ that could not be found.
|
缓存 Java Maven
严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderLis
严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderLis
107 0
严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderLis
|
Java Maven
严重: Error configuring application listener of class org.springframework.web.util.Log4jConfigListener
严重: Error configuring application listener of class org.springframework.web.util.Log4jConfigListener
111 0
严重: Error configuring application listener of class org.springframework.web.util.Log4jConfigListener
|
Java 应用服务中间件
严重: Exception sending context destroyed event to listener instance of class org.springframework.web.
严重: Exception sending context destroyed event to listener instance of class org.springframework.web.
194 0
严重: Exception sending context destroyed event to listener instance of class org.springframework.web.

热门文章

最新文章