您的位置:首页 > 理论基础 > 计算机网络

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时也会报错。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐