首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring @RequestBody中的对象列表生成“语法错误的请求”

Spring @RequestBody中的对象列表生成“语法错误的请求”
EN

Stack Overflow用户
提问于 2014-08-25 11:48:56
回答 1查看 1.9K关注 0票数 0

我试图使用Spring + Swagger注释通过REST服务更新对象。该方法如下所示:

代码语言:javascript
复制
@ApiOperation(value = "Modifies the entity")
@RequestMapping(value = "/entity", method = RequestMethod.PUT, headers = "Accept=application/json")
@APIMonitor
@ResponseBody
public PubTagger saveEntityDetails(
        HttpServletResponse response,
        ModelMap model,
        @RequestBody final EntityClass entityInfo
        )
        throws Exception {
         ...
        }

实体的定义是:

代码语言:javascript
复制
{
  "id": "long",  
  "description": "string",
  "name": "string",
  "properties": [
    {
      "name": "string",
      "value": "string"
    }
  ]
}

它给了我一个错误

客户端发送的请求在语法上是不正确的()

但只有在填充Properties字段中的对象时才会发生这种情况。如果我把它空着,它就成功了。因此,我推断Spring中嵌套的对象在列表中有问题。

这里有我遗漏的东西吗?我是否必须在模型中指定任何内容才能使其工作?

编辑: Posting实体类

代码语言:javascript
复制
public class Entity {
    private Long id;
    private String name;
    private String description;
    private List<Property> properties = new ArrayList<>();

    public void setId(final Long id) {
        this.id = id;
    }

    public Entity() {
        super();
    }

    public String getName() {
        return name;
    }
    public void setName(final String name) {
        this.name = name;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(final String description) {
        this.description = description;
    }

    public List<Property> getProperties() {
        return properties;
    }

    public void setProperties(List<Property> properties) {
        this.properties = properties;
    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-25 19:42:57

谢谢,我发现了错误。

类属性只具有参数化构造函数,没有默认构造函数,因此无法将JSON requestBody配置为对象。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25485245

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档