java - Spring MVC @RequestBody 中的对象列表生成 "Syntactically incorrect request"

标签 java rest spring-mvc put

我正在尝试使用 Spring MVC + Swagger 注释通过 REST 服务更新对象。 方法是这样的:

@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 {
         ...
        }

实体定义是:

{
  "id": "long",  
  "description": "string",
  "name": "string",
  "properties": [
    {
      "name": "string",
      "value": "string"
    }
  ]
}

它给了我一个错误

The request sent by the client was syntactically incorrect ()

但只有当我填充 Properties 字段中的对象时才会发生这种情况。如果我将其留空,它就会成功。所以我推断 Spring MVC 中的列表内嵌套对象有问题。

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

编辑:发布实体类

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;
    }

}

最佳答案

谢谢,我发现错误了。

正是Property类只有参数化构造函数,没有默认构造函数,导致无法将JSON requestBody编码到对象中。

关于java - Spring MVC @RequestBody 中的对象列表生成 "Syntactically incorrect request",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25485245/

相关文章:

Java Simple EchoServer 无法工作

rest - 使用 go API 在 blobstore 上上传文件

jquery - 通过json进行REST api认证

javascript - Moment 和 LocalDate 之间的转换减去日

java - JPanel 添加到容器时只显示一个组件

java - 如何使用 ajax 请求从 spring Controller 返回 View ?

java - 应用程序上下文加载很久以前在项目中删除的 Bean

rest - Stripe API : What's my account ID? 如何在 GET/v1/accounts/中默认它?

java - 400 错误请求 - 将 JSON 数据发布到 RESTful Controller 时

spring - 向 ModelAndView 添加属性