Spring Rest POST不支持Json RequestBody内容类型 [英] Spring Rest POST Json RequestBody Content type not supported

查看:1331
本文介绍了Spring Rest POST不支持Json RequestBody内容类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用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

如果我尝试相同的请求只是改变请求对象。它有效。

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我替换为当àHibernitify我的对象时整数。

My class had id property int that I replaced with Integer when à Hibernitify my object.

但显然,我忘了删除setter而且我有:

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时,休息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:


无法评估类型[simple type,
class your.package.ClassName]的Jackson反序列化:
com.fasterxml.jackson.databind.JsonMappingException:冲突
setter定义对于属性propertyname

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆