不支持 Spring Rest POST Json RequestBody 内容类型 [英] Spring Rest POST Json RequestBody Content type not supported
问题描述
当我尝试使用 post 方法发布新对象时.RequestBody 无法识别 contentType.Spring 已经配置好并且 POST 可以与其他对象一起工作,但不是这个特定的对象.
When I try to post new object with post method. RequestBody could not recognize contentType. Spring is already configured and POST could work with others objects, but not this specific one.
org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported
如果我尝试相同的请求,只需更改 requestbody 对象.它有效.
If I try the same request just changing requestbody object. It works.
推荐答案
我找到了解决方案.这是因为我有两个同名但类型不同的二传手.
I found solution. It's was because I had 2 setter with same name but different type.
我的班级有 id 属性 int,当我对我的对象进行休眠时,我将其替换为 Integer.
My class had id property int that I replaced with Integer when à Hibernitify my object.
但显然,我忘记删除二传手,我有:
But apparently, I forgot to remove setters and I had :
/**
* @param id
* the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @param id
* the id to set
*/
public void setId(Integer id) {
this.id = id;
}
当我移除这个 setter 时,rest resquest 工作得很好.
When I removed this setter, rest resquest work very well.
Intead 抛出解组错误或反映类错误.异常 HttpMediaTypeNotSupportedException 在这里接缝真的很奇怪.
Intead to throw unmarshalling error or reflect class error. Exception HttpMediaTypeNotSupportedException seams really strange here.
我希望这个 stackoverflow 可以帮助其他人.
I hope this stackoverflow could be help someone else.
您可以检查您的 Spring 服务器 控制台是否有以下错误消息:
You can check your Spring server console for the following error message:
未能评估类型 [简单类型,class your.package.ClassName]:com.fasterxml.jackson.databind.JsonMappingException:冲突属性propertyname"的 setter 定义
Failed to evaluate Jackson deserialization for type [simple type, class your.package.ClassName]: com.fasterxml.jackson.databind.JsonMappingException: Conflicting setter definitions for property "propertyname"
那么您可以确定您正在处理上述问题.
Then you can be sure you are dealing with the issue mentioned above.
这篇关于不支持 Spring Rest POST Json RequestBody 内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!