我正在尝试使用 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/