Spring Data Rest 2.1.0 Unable to POST or PUT Complex Resource

EDIT: This also happens with PUT.

Using spring -data-rest-webmvc version 2.1.0.BUILD-SNAPSHOT I found that I cannot POST a resource with a relation pointing to an existing resource. I have 2 such objects that require a link to instantiate, and POST for any of their endpoints leads to the following behavior.

Replenishment of the resource without the necessary links works well.

I dug a little, and it turned out that PersistentEntityResourceHandlerMethodArgumentResolver perfectly finds MappingJackson2HttpMessageConverter , but in the end it throws an exception, checking if ObjectMapper deserialize the type. The cause of the exception is a NullPointerException .

Example POST with relationship / reservation:

{ "description" : "test_post", "dateMade" : "2014-03-03T08:04:44.293-0600", "timeLastChanged" : null, "userLastChanged" : null, "courseTypeId" : null, "numCredits" : null, "instructor" : null, "numParticipants" : null, "reservationType" : "MCU", "status" : "REQUESTED", "abstract" : null, "requestor" : "http://localhost:8080/users/2", "submitter" : "http://localhost:8080/users/2", "conferences" : [] }

ANSWER:

{ cause: null message: "No suitable HttpMessageConverter found to read request body into object of type class domain.Reservation from request with content type of application/json!" }

POST without links to / roomGroups:

{ "description" : "All Rooms", "isOffNetwork" : false, "roomGroupType" : "STANDARD" }

ANSWER:

201 Created

Something is wrong in JSON, I'm POSTING, which leads to NPE from ObjectMapper ? Is there any workaround? This worked for me in 2.0.0.RC1, using a slightly different scheme for including link references in JSON, and since Jackson's dependency version remained the same, I wonder what causes this problem ...

Thanks for any help!

UPDATE:

Now this problem is not related to related objects ...

I created a new @Entity ConnectionRequest as follows:

 @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "CONNECTION_REQUEST_ID") private Long id; @Column(name = "FROM_ENTITY_ID", nullable = false) private Long fromId; @Column(name = "TO_ENTITY_ID", nullable = false) private Long toId; @Convert(converter = EntityTypeConverter.class) @Column(name = "FROM_ENTITY_TYPE_ID", nullable = false) private EntityType fromType; @Convert(converter = EntityTypeConverter.class) @Column(name = "TO_ENTITY_TYPE_ID", nullable = false) private EntityType toType; @ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.MERGE}) @JoinColumn(name = "CONFERENCE_ID", nullable = false) private Conference conference; 

I can send a new ConnectionRequest entry with the Conference relation included in json as such {"conference" : ".../conferences/1"} .

However, I still get the same exception w / this @Entity Reservation:

 @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "RESERVATION_ID") private Long id; @Column(name = "DESCRIPTION", length = 50, nullable = false) private String description; @Temporal(TemporalType.TIMESTAMP) @Column(name = "DATE_MADE", nullable = false) private Date dateMade; @Temporal(TemporalType.TIMESTAMP) @Column(name = "TIME_LAST_CHANGED") private Date timeLastChanged; @Column(name = "USER_LAST_CHANGED") private Integer userLastChanged; // TODO why is this an int? @Column(name = "ABSTRACT", length = 2000) private String _abstract; @Column(name = "COURSE_TYPE_ID") private Integer courseTypeId; @Column(name = "NUMBER_OF_CREDITS") private Integer numCredits; @Column(name = "INSTRUCTOR", length = 255) private String instructor; @Column(name = "NUMBER_OF_PARTICIPANTS") private Integer numParticipants; @Convert(converter = ReservationTypeConverter.class) @Column(name = "RESERVATION_TYPE_ID", nullable = false) private ReservationType reservationType; @Convert(converter = StatusConverter.class) @Column(name = "STATUS_ID") private Status status; @ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.MERGE}) @JoinColumn(name="REQUESTOR_USER_ID", nullable=false) private User requestor; @ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.MERGE}) @JoinColumn(name="SUBMITTER_USER_ID", nullable=false) private User submitter; @OneToMany(fetch = FetchType.LAZY, mappedBy = "reservation", cascade = {CascadeType.REMOVE}) private Set<Conference> conferences = new HashSet<>(); 

I'm not sure what is special about this class that causes me problems ...

0
source share
1 answer

The problem was this:

Both non-writable objects had a property called _abstract due to the fact that it is a reserved word in Java. I called getter and setter for this property getAbstract() and setAbstract() respectively.

Jackson seems to throw a null pointer exception because the getter and setter do not match the property name as expected.

When I changed the name of the property to resvAbstract and updated the accessors to getResvAbstract() and setResvAbstract() , everything setResvAbstract() together and started working.

I am still interested in learning about the changes that led to this problem, but I'm glad that it works!

0
source

All Articles