HttpMediaTypeNotSupportedException: Content type 'application/json;' not supported 问题原因之一
2014-12-24 18:22
423 查看
方法配置:
@RequestMapping(value = "/check" ,method = RequestMethod.POST)
@ResponseBody
public Object check(@RequestBody A a)
错误:
org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported
网上看到有很多情况会导致这个问题,这里我说一下我这里的问题原因。
看Spring的源码:
protected <T> Object readWithMessageConverters(HttpInputMessage inputMessage, MethodParameter methodParam, Class<T> paramType) throws IOException,
HttpMediaTypeNotSupportedException {
MediaType contentType = inputMessage.getHeaders().getContentType();
if (contentType == null) {
contentType = MediaType.APPLICATION_OCTET_STREAM;
}
for(HttpMessageConverter<?> messageConverter : this.messageConverters) {
if (messageConverter.canRead(paramType, contentType)) {
if (logger.isDebugEnabled()) {
logger.debug("Reading [" + paramType.getName() + "] as \"" + contentType + "\" using [" + messageConverter + "]");
}
return ((HttpMessageConverter<T>) messageConverter).read(paramType, inputMessage);
}
}
throw new HttpMediaTypeNotSupportedException(contentType, allSupportedMediaTypes);
}
public boolean canRead(Class<?> clazz, MediaType mediaType) {
return supports(clazz) && canRead(mediaType);
}
经分析应该是canRead返回了false, 记得之前碰到一个问题是bean里面属性的get,set方法不规范导致json转换报错,后面检查代码发现,这次是同样的问题,在A里面引用了B类,B里面有一个属性c,有两个setC方法,参数不一样,是重载的方法,这个方法重载导致json转换不支持,然后canRead方法了false.
同样,如果你的bean里面有get或set方法,但是没有这个属性,比如你有一个getName方法,但是bean里面没有name这个属性,在转换json时也会报错。
@RequestMapping(value = "/check" ,method = RequestMethod.POST)
@ResponseBody
public Object check(@RequestBody A a)
错误:
org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported
网上看到有很多情况会导致这个问题,这里我说一下我这里的问题原因。
看Spring的源码:
protected <T> Object readWithMessageConverters(HttpInputMessage inputMessage, MethodParameter methodParam, Class<T> paramType) throws IOException,
HttpMediaTypeNotSupportedException {
MediaType contentType = inputMessage.getHeaders().getContentType();
if (contentType == null) {
contentType = MediaType.APPLICATION_OCTET_STREAM;
}
for(HttpMessageConverter<?> messageConverter : this.messageConverters) {
if (messageConverter.canRead(paramType, contentType)) {
if (logger.isDebugEnabled()) {
logger.debug("Reading [" + paramType.getName() + "] as \"" + contentType + "\" using [" + messageConverter + "]");
}
return ((HttpMessageConverter<T>) messageConverter).read(paramType, inputMessage);
}
}
throw new HttpMediaTypeNotSupportedException(contentType, allSupportedMediaTypes);
}
public boolean canRead(Class<?> clazz, MediaType mediaType) {
return supports(clazz) && canRead(mediaType);
}
经分析应该是canRead返回了false, 记得之前碰到一个问题是bean里面属性的get,set方法不规范导致json转换报错,后面检查代码发现,这次是同样的问题,在A里面引用了B类,B里面有一个属性c,有两个setC方法,参数不一样,是重载的方法,这个方法重载导致json转换不支持,然后canRead方法了false.
同样,如果你的bean里面有get或set方法,但是没有这个属性,比如你有一个getName方法,但是bean里面没有name这个属性,在转换json时也会报错。
相关文章推荐
- HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported
- ajax使用向Spring MVC发送JSON数据出现 org.springframework.web.HttpMediaTypeNotSupportedException: Content type
- 在Spring Cloud框架整合微服务时出现的org.springframework.web.HttpMediaTypeNotSupportedException异常问题
- HttpMediaTypeNotAcceptableException: Could not find acceptable representation原因及解决方法
- 关于Spring3 MVC的 HttpMediaTypeNotSupportedException
- HttpMediaTypeNotSupportedException
- org.springframework.web.HttpMediaTypeNotSupportedException: Unsupported Media Type, status=415
- 关于Spring3 MVC的 HttpMediaTypeNotSupportedException
- spring boot HttpMediaTypeNotSupportedException
- Spring Boot: HttpMediaTypeNotAcceptableException: Could not find acceptable representation原因及解决方法
- springmvc @ResponseBody HttpMediaTypeNotAcceptableException
- Handler execution resulted in exception: Content type 'application/json;charset=UTF-8' not supported
- 对与几种javax.el.PropertyNotFoundException: Property 'XXX' not found on type java.lang.String问题原因
- org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PUT' not supported
- java.io.FileNotFoundException: http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd问题终结原因
- org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representatio
- org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported解决!
- org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
- org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representatio
- spring 4 异常HttpMediaTypeNotAcceptableException解决方案