Reputation: 8935
I am using Java8 with Spring, Hibernate and JPA.
I have a RESTful service that saves a Person
object:
@RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody ResponseEntity<Person> savePerson(@RequestBody Person person) {
try {
person = personService.save(person);
return new ResponseEntity<Person>(person, HttpStatus.OK);
} catch (Exception e) {
Person p = null;
return new ResponseEntity<Person>(p, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
I get the following error when trying to merge
a Person
object:
16:02:58,472 WARN [org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] (default task-11) Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: N/A at [Source: java.io.PushbackInputStream@7583d44c; line: 29, column: 3] (through reference chain: com.jobs.spring.domain.Person["blockedPersons"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: N/A at [Source: java.io.PushbackInputStream@7583d44c; line: 29, column: 3] (through reference chain: com.jobs.spring.domain.Person["blockedPersons"])
I guess because it talks about a "reference chain
", I may have some recursion error. Or for some reason it is struggling to parse the Java into the JSON object.
If anyone can advise, I would appreciate the help.
More info:
Person.java
@Entity
@Table(name = "person")
@XmlRootElement(name = "person")
public class Person extends AbstractDomain<Long> {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
@JoinTable(name = "person_blocked", joinColumns = {
@JoinColumn(name = "PER_ID", referencedColumnName = "ID") }, inverseJoinColumns = {
@JoinColumn(name = "BLOCK_ID", referencedColumnName = "ID", unique = true) })
private Set<BlockedPerson> blockedPersons = null;
...
}
BlockedPerson.java
@Entity
@Table(name = "blocked_person")
@XmlRootElement(name = "blocked_person")
public class BlockedPerson extends AbstractDomain<Integer> {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "ID", unique = true, nullable = false)
private Integer id;
@Column(name = "BLOCKED_PER_ID", nullable = false)
private Long blockedPersonId;
@XmlElement
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@XmlElement
public Long getBlockedPersonId() {
return blockedPersonId;
}
public void setBlockedPersonId(Long blockedPersonId) {
this.blockedPersonId = blockedPersonId;
}
}
Upvotes: 0
Views: 5722
Reputation: 8935
Solution
The reson for the error above is because I was getting an exception in the setter method on Person
which was doing the following on a null
object.
this.blockedPersons.clear();
Fix:
change:
private Set<BlockedPerson> blockedPersons = null;
to:
private Set<BlockedPerson> blockedPersons = new HashSet<BlockedPerson>();
Upvotes: 1