简体   繁体   English

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

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

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.

相关问题 如何使用spring-data-rest来发布新的嵌套实体 - How to POST new nested entities using spring-data-rest Spring-Data-Rest验证器 - Spring-Data-Rest Validator Curl不适用于HTTP发布到Spring数据休息的RequestMapping - Curl not working for HTTP Post to Spring-data-rest RequestMapping spring-data-rest 中具有不同签名的相同存储库方法 - Same repository method with different signature in spring-data-rest 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> 使用spring-data-rest返回所有结果 - returning all result using spring-data-rest 杰克逊嵌套对象反序列化为属性 - Jackson Nested Object Deserialization into property Java Jackson嵌套对象的反序列化 - Java Jackson Deserialization of Nested Objects 限制spring-data-rest返回的文档 - Restrict documents returned by spring-data-rest 丰富spring-data-rest存储库上的行为 - Enrich behavior on spring-data-rest repository
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM