19

Relates to: Spring Data Rest version 2.0.2

I'm trying to POST an entity (Address) with a @ManyToOne (instead of @OneToOne as in example) relationship to Person as explained in: Embedded Entity references in complex object graphs but I get a Jackson Error:

org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: (was java.lang.NullPointerException) (through reference chain: Address["person"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: Address["person"])
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readJavaType(MappingJackson2HttpMessageConverter.java:228) ~[spring-web-4.0.3.RELEASE.jar:4.0.3.RELEASE]

I find it strange that I can send a JSON object with rel and href in the "person" attribute of Address like:

"person": {
    "rel" : "people.Person",
    "href" : "http://localhost:8080/people/1"
}

Is this still valid? The refered page is quite old (2012). Are there any updated docs. I have reviewed the reference doc even for latest (to date) 2.1.0 RC1, but I find it lacking.

Any pointer would be appreciated.

Stackoverflow suggested me to see this question, which, in my case fail with a HTTP Status 405 - Request method 'POST' not supported ... which is probably a configuration issue, but my original question stands.

Update 1: From the Reference Doc section 4.1 it says:

Sometimes the behavior of the Spring Data REST's ObjectMapper, which has been specially configured to use intelligent serializers that can turn domain objects into links and back again, may not handle your domain model correctly. There are so many ways one can structure your data that you may find your own domain model isn't being translated to JSON correctly. It's also sometimes not practical in these cases to try and support a complex domain model in a generic way. Sometimes, depending on the complexity, it's not even possible to offer a generic solution.

The problem could be that I'm using a custom Jackson ObjectMapper with Hibernate support. I'll try to dig further.

Update 2: Never mind update 1, I removed the Hibernate4Module and the behaviour is the same.

4
  • You should try to surround the {} with "" Commented May 20, 2014 at 18:39
  • @ShayElkayam ??? Would you care to elaborate... Commented May 20, 2014 at 18:56
  • did you configure your JPA annotations as shown here?? Java Persistence/ManyToOne Commented Dec 23, 2014 at 21:02
  • I'm trying to accomplish the same (see here) but it's not working. Would you mind sharing how you created the repositories etc. in order to make this work? Commented Nov 19, 2017 at 13:06

2 Answers 2

19

The format on the page you linked to was out of date. I just updated it. The correct format is:

{
    "postalCode": "12345",
    "province": "MO",
    "lines": ["1 W 1st St."],
    "city": "Univille",
    "person": "http://localhost:8080/people/1"
}

At least, this is what works for me with my entities.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm struggling to get this to work (see here). Am I missing something?
Sorry @displayname - I am not using Spring anymore and everything I've learned has been paged out of memory. :/
For the value of the person, you can use /1 instead of http://localhost:8080/people/1
0

@JBCP’s answer is correct. Not directly related to the answer: Just keep one thing in mind that spring doesn’t check if the resource 1 exists. If it doesn’t exist you’ll get null in the field and no exception.

2 Comments

Any way to throw an exception if the related resource does not exist?
@NeerajVernekar One way would be to annotate the field with NotNull in your code and get the exception that way. I don't know how to configure Spring to throw a 500 for the post method, because the relation http://localhost:8080/people/1 for one of the fields doesn't exist ....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.