大家好!我有这样的问题
@RequestMapping(value = "/createrobot", method = RequestMethod.POST)
public Robot createRobot(
@RequestParam(value="id", required = false, defaultValue = "-") String id,
@RequestParam(value="name", required = false, defaultValue = "-") String name,
@RequestParam(value="age", required = false, defaultValue = "-") String age,
@RequestBody Robot robot){
return new Robot(1, "Maks", "456");
}
我在 POSTMAN http://localhost:8081/supercontroller/createrobot中发出请求
我通过 Json
{
"id": 78,
"name": "-",
"age": "-"
}
发生错误:
{
"timestamp": "2018-06-18T11:57:02.825+0000",
"status": 500,
"error": "Internal Server Error",
"message": "Type definition error: [simple type, class ru.asdf.Robot]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `ru.asdf.Robot` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)\n at [Source: (PushbackInputStream); line: 2, column: 5]",
"path": "/supercontroller/createrobot"
}
我看到问题出在反序列化中,我该如何解决?有一个机器人实体
public class Robot {
Integer id;
String name;
String age;
public Robot(int id, String name, String age) {
this.id = id;
this.name = name;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
使用什么注释?
这个问题隐藏在没有没有参数的构造函数的情况下。添加后,错误消失了。