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"

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

org.springframework.web.HttpMediaTypeNotSupportedException: Unsupported Media Type, status=415_第1张图片

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

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

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

参考: 

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

另见下面的 AJAX 调用示例:





测试 type=Unsupported Media Type, status=415 问题








	

测试 type=Unsupported Media Type, status=415 问题

指定:
contentType:"application/json"



你可能感兴趣的:(JAVA,SpringBoot)