Spring-data-rest jacksonHttpMessageConverter doesn't convert nested entity

96 Views Asked by At

I am using spring-data-rest-webmvc:2.5.6.RELEASE. My application uses auto configuration to expose all endpoints simple I didn't provide any custom configuration. Consider the following domain classes

Order.java

public class Order {

    @Id @GeneratedValue//
    private Long id;
    @ManyToOne(fetch = FetchType.LAZY)//
    private Person creator;
    private String type;

    public Order(Person creator) {
        this.creator = creator;
    }

    // getters and setters
}

Person.java

pubic class Person {

    @Id @GeneratedValue private Long id;

    @Description("A person's first name") //
    private String firstName;

    @Description("A person's last name") //
    private String lastName;

    @Description("A person's siblings") //
    @ManyToMany //
    private List<Person> siblings = new ArrayList<Person>();

    @ManyToOne //
    private Person father;

    @Description("Timestamp this person object was created") //
    private Date created;

    @JsonIgnore //
    private int age;

    private int height, weight;
    private Gender gender;

    // ... getters and setters
}

I am using a following curl command to POST a order

    curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ \ 
   "creator": { \ 
     "firstName": "John", \ 
     "lastName": "Smith", \ 
     "age": 1 \ 
   }
 }' 'http://localhost:8080/orders'

The server throws 500 Internal Server Error. Because order.creator=null in hibernate insert query. But if I modify my curl request body as follows

curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ \ "creator": "http://localhost:8080/creators/1" }' http://localhost:8080/orders'

the server returns 200 as expected. But I don't want to use the 2nd curl command, since my existing application requests with nested/expanded json entity in request body.

Is there any configuration exists in spring-data-rest to make the 1st curl command successfull? or

Is this an issue with spring-data-rest?

0

There are 0 best solutions below