[英]Spring-data-rest POST using Jackson Deserialization with same property name in nested objects
I am using spring-boot-starter-parent 1.3.3 and jackson-core-asl:jar:1.9.2. 我正在使用spring-boot-starter-parent 1.3.3和jackson-core-asl:jar:1.9.2。 I am unable to create the object (Group) with person associated in it, because the group is created with the person name. 我无法创建具有相关人员的对象(组),因为该组是使用人员名称创建的。 The response likes the following.. 响应如下所示。
eg Request: 例如:
{
"name": "Students"
"person": {"id": 1, "name: "John"}
}
Response: 响应:
{
"id" : 1,
"name" : "John",
"content" : [ ],
"links" : [ {
"rel" : "self",
"href" : "http://localhost/Group/1"
}, {
"rel" : "person",
"href" : "http://localhost/Group/1/person"
} ]
}
The Group (name: "Students") was created with person name "John" in the above response. 在上面的响应中,使用人名“ John”创建了组(名称:“ Students”)。
Person.java 人.java
@Table(name = "person")
public class Person implements Serializable {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private int id;
private String name;
//getter & setter
Group.java Group.java
@Table(name = "group")
public class Group implements Serializable {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private int id;
private String name;
@ManyToOne
@JoinColumn(name = "person_id")
private Person person;
//getter & setter
If I put @JsonProperty() in Group.java, everything works fine. 如果我将@JsonProperty()放在Group.java中,则一切正常。
eg 例如
Group.java Group.java
@Table(name = "group")
public class Group implements Serializable {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private int id;
private String name;
@ManyToOne
@JoinColumn(name = "person_id")
@JsonProperty("person") // why this is needed??
private Person person;
The Default json property is same as the field name then Why this @JsonProperty annotation needed here? Default json属性与字段名称相同,然后为什么这里需要此@JsonProperty批注?
Is there any configuration exists in object mapper to solve this problem? 对象映射器中是否存在任何配置可以解决此问题?
And also if I put @RestResource(exported = false) instead of "@JsonProperty", It works fine in test cases but fails to create through swaggerUI. 并且如果我将@RestResource(exported = false)代替“ @JsonProperty”,它在测试用例中也可以正常工作,但无法通过swaggerUI创建。
Getting the following error.. 收到以下错误。
{ "cause": { "cause": null, "message": "Can not instantiate value of type [simple type, class xx.xxx.Person] from String value (' http://localhost:8080/persons/1 '); no single-String constructor/factory method\\n at [Source: org.apache.catalina.connector.CoyoteInputStream@64bf4540; line: 18, column: 19] (through reference chain: xx.xxx.Group[\\"person\\"])" }, "message": "Could not read document: Can not instantiate value of type [simple type, class xx.xxx.Person] from String value (' http://localhost:8080/persons/1 '); no single-String constructor/factory method\\n at [Source: org.apache.catalina.connector.CoyoteInputStream@64bf4540; line: 18, column: 19] (through reference chain: xx.xxx.[\\"person\\"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class xx.xx.Person] from String value (' http://localhost:8080/persons/1 '); no single-String constructor/factory method\\n at [Source: org.apache.catalina.connector.Coyot {“ cause”:{“ cause”:null,“ message”:“无法从字符串值(' http:// localhost:8080 / persons / 1中实例化类型[简单类型,类xx.xxx.Person]的值');在[来源:org.apache.catalina.connector.CoyoteInputStream@64bf4540;行:18,列:19]中没有单字符串构造函数/工厂方法\\ n(通过引用链:xx.xxx.Group [\\“ person \\“])”},“消息”:“无法读取文档:无法从字符串值(' http:// localhost:8080 / persons /中实例化类型[简单类型,类xx.xxx.Person]的值1 ');在[来源:org.apache.catalina.connector.CoyoteInputStream@64bf4540;没有行:18,列:19]中没有单字符串构造函数/工厂方法\\ n(通过引用链:xx.xxx。[\\“ person \\“]);嵌套的异常是com.fasterxml.jackson.databind.JsonMappingException:无法从字符串值实例化类型[简单类型,类xx.xx.Person]的值(' http:// localhost:8080 / persons / 1 ');在[来源:org.apache.catalina.connector.Coyot中没有单字符串构造函数/工厂方法\\ n eInputStream@64bf4540; eInputStream @ 64bf4540; line: 18, column: 19] (through reference chain: xx.xxx.Group[\\"person\\"])" } 行:18,列:19](通过参考链:xx.xxx.Group [\\“ person \\”])“}
Kindly provide your thoughts. 请提供您的想法。
check class name in Person.java, class name is Group instead of Person 在Person.java中检查类名,类名是Group而不是Person
public class Group implements Serializable { 公共类组实现了Serializable {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.