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

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" οnclick="doRequest()">

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



  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'xxx' not supported] 这个异常表示Spring MVC在处理请求时不支持指定的内容类型。 在Spring MVC中,我们可以通过@RequestMapping注解来指定接受的内容类型,例如: ```java @RestController @RequestMapping(value = "/api", produces = MediaType.APPLICATION_JSON_VALUE) public class ApiController { @GetMapping(value = "/data", consumes = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<String> getData(@RequestBody DataRequest requestData) { // 处理数据请求 return ResponseEntity.ok("success"); } } ``` 在上面的代码中,使用了@RequestMapping注解来指定接受和返回的内容类型。produces属性用于指定返回的内容类型,consumes属性用于指定接受的内容类型。在这个例子中,我们接受的内容类型是APPLICATION_JSON_VALUE,返回的内容类型也是APPLICATION_JSON_VALUE。 当客户端发送一个请求时,如果请求的内容类型不支持,就会抛出HttpMediaTypeNotSupportedException异常。我们需要对这个异常进行处理,下面是一个解决方法: ```java @RestControllerAdvice public class CustomExceptionHandler { @ExceptionHandler(HttpMediaTypeNotSupportedException.class) public ResponseEntity<String> handleHttpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException ex) { return ResponseEntity.status(HttpStatus.UNSUPPORTED_MEDIA_TYPE).body("不支持的内容类型"); } } ``` 我们可以通过编写一个全局异常处理类,使用@RestControllerAdvice和@ExceptionHandler注解来处理抛出的异常。在上面的代码中,handleHttpMediaTypeNotSupportedException方法用于处理HttpMediaTypeNotSupportedException异常,返回一个表示不支持内容类型的响应实体。 通过上述方法,我们可以解决[org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'xxx' not supported]异常。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值