Java 找不到合适的创建者方法从JSON字符串反序列化
我在Spring应用程序中使用Jackson。当Jackson将我的JSON数据转换为对象时,它会给我一个错误 我的JSON数据是:Java 找不到合适的创建者方法从JSON字符串反序列化,java,spring,deserialization,jackson,Java,Spring,Deserialization,Jackson,我在Spring应用程序中使用Jackson。当Jackson将我的JSON数据转换为对象时,它会给我一个错误 我的JSON数据是: {“名称”:“sdfg”,“用户名”:“dfgdg”,“密码”:“dfgdg”,“类型”:“A”,“协议”:“1”,“说明”:“sdfsdfdsf”} 我的班级是: public class Device extends Name { @Column private String username; @Column privat
{“名称”:“sdfg”,“用户名”:“dfgdg”,“密码”:“dfgdg”,“类型”:“A”,“协议”:“1”,“说明”:“sdfsdfdsf”}
我的班级是:
public class Device extends Name {
@Column
private String username;
@Column
private String password;
@JsonUnwrapped// I added this tag, not sure to use
@ManyToOne
private DeviceType type;
@JsonUnwrapped// I added this tag, not sure to use
@ManyToOne
private Protocol protocol;
...
//description and name are fields at Name that Device extends
//getters and setters
/* I tried that not sure:
@JsonProperty("protocol")
public void setProtocol(Protocol protocol) {
this.protocol = protocol;
}
*/
}
public class DeviceType extends Name {
private List<Protocol> supportedProtocols;
public List<Protocol> getSupportedProtocols() {
return supportedProtocols;
}
/*
I tried to add that:
@JsonProperty("type")
@Override
public void setName(String name) {
super.setName(name);
}
*/
}
public class Protocol extends Name {
/*
Nothing at this method. I added that for my purpose:
JsonProperty("protocol")
@Override
public void setName(String name) {
super.setName(name);
}
*/
}
异常准确地解释了问题所在:它不知道如何将字符串“A”转换为DeviceType类型的值。 最简单的方法是添加构造函数,如:
public DeviceType(String type) { .... }
一切都会好起来的。试试这个,它会好起来的
{
"name": "sdfg",
"username": "dfgdg",
"password": "dfgdg",
"type": {"type_id":"1"},
"protocol": {"protocol_id":"1"}
}
也许这是复制品?
{
"name": "sdfg",
"username": "dfgdg",
"password": "dfgdg",
"type": {"type_id":"1"},
"protocol": {"protocol_id":"1"}
}