SpringMVC源码415 (Unsupported Media Type) 缘由及解决方法
在Spring MVC中,当客户端要求的媒体类型不被服务器支持时,会返回一个415 (Unsupported Media Type)毛病。这个毛病通常产生在客户端要求的Content-Type与服务器支持的媒体类型不匹配时。
缘由:
解决方法:
另外,还需要注意的是,如果服务器支持多个媒体类型,可以通过使用produces属性来指定返回的媒体类型。例如,在RestController的@RequestMapping注解中添加produces属性,指定返回的媒体类型:
@RestController
@RequestMapping(value = "/example", produces = "application/json")
public class ExampleController {
// ...
}
这样,当客户端要求该接口时,服务器将只返回application/json类型的数据,如果客户端要求的媒体类型与之不匹配,将返回415毛病。
TOP