[英]Spring-data-rest POST using Jackson Deserialization with same property name in nested objects
我正在使用spring-boot-starter-parent 1.3.3和jackson-core-asl:jar:1.9.2。 我無法創建具有相關人員的對象(組),因為該組是使用人員名稱創建的。 響應如下所示。
例如:
{
"name": "Students"
"person": {"id": 1, "name: "John"}
}
響應:
{
"id" : 1,
"name" : "John",
"content" : [ ],
"links" : [ {
"rel" : "self",
"href" : "http://localhost/Group/1"
}, {
"rel" : "person",
"href" : "http://localhost/Group/1/person"
} ]
}
在上面的響應中,使用人名“ John”創建了組(名稱:“ Students”)。
人.java
@Table(name = "person")
public class Person implements Serializable {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private int id;
private String name;
//getter & setter
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
如果我將@JsonProperty()放在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;
Default json屬性與字段名稱相同,然后為什么這里需要此@JsonProperty批注?
對象映射器中是否存在任何配置可以解決此問題?
並且如果我將@RestResource(exported = false)代替“ @JsonProperty”,它在測試用例中也可以正常工作,但無法通過swaggerUI創建。
收到以下錯誤。
{“ 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; 行:18,列:19](通過參考鏈:xx.xxx.Group [\\“ person \\”])“}
請提供您的想法。
在Person.java中檢查類名,類名是Group而不是Person
公共類組實現了Serializable {
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.