簡體   English   中英

在嵌套對象中使用具有相同屬性名稱的Jackson反序列化的Spring-data-rest POST

[英]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 {

Jackson (Java):反序列化相同的屬性名但返回不同的對象。 其中一個返回 object 和第二個列表<object><div id="text_translate"><p>我有一個這樣的 POJO:</p><pre> public class NewClass { String name; @JsonProperty("productType") ProductType productType2005; List&lt;ProductType&gt; productType; }</pre><p> 我想將 json 反序列化為 Pojo。 問題是我的屬性名稱相同 productType 但我可以期待兩種不同的返回類型或數據結構。</p><ol><li> 返回ProductType</li><li> return List&lt;ProductType&gt;因為屬性名稱相同我如何有效地使用 Jackson 注釋來解決它?</li></ol><p> 我使用 rest-assured 進行反序列化,使用 Lombok 進行典型的 getter 和 setter。</p></div></object>

[英]Jackson (Java) : deserialization for the same property name but return different objects. for one it return object and second List<Object>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM