Spring-data-rest POST using Jackson Deserialization with same property name in nested objects

1k views Asked by At

I am using spring-boot-starter-parent 1.3.3 and 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..

e.g 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.

Person.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

If I put @JsonProperty() in Group.java, everything works fine.

e.g

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?

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.

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.CoyoteInputStream@64bf4540; line: 18, column: 19] (through reference chain: xx.xxx.Group[\"person\"])" }

Kindly provide your thoughts.

1

There are 1 answers

0
Chandra On

check class name in Person.java, class name is Group instead of Person

public class Group implements Serializable {