当我尝试使用 post 方法发布新对象时。 RequestBody 无法识别 contentType。 Spring 已经配置好,POST 可以与其他对象一起使用,但不是这个特定的对象。
org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported
如果我尝试相同的请求,只需更改 requestbody 对象。它有效。
最佳答案
我找到了解决方案。 这是因为我有两个同名但类型不同的二传手。
我的类(class)有 id 属性 int,当我 hibernate 我的对象时,我将其替换为 Integer。
但显然,我忘了删除 setter,我有:
/**
* @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 工作得很好。
打算抛出解码错误或反射(reflect)类错误。异常 HttpMediaTypeNotSupportedException 在这里接缝真的很奇怪。
我希望这个 stackoverflow 可以帮助其他人。
旁注
您可以检查您的 Spring 服务器 控制台以查看以下错误消息:
Failed to evaluate Jackson deserialization for type [simple type, class your.package.ClassName]: com.fasterxml.jackson.databind.JsonMappingException: Conflicting setter definitions for property "propertyname"
那么您可以确定您正在处理上述问题。
关于java - 不支持 Spring Rest POST Json RequestBody 内容类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19444855/