0

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;
    }

}
9
  • It would be easier if you would show us Person class. Commented Jul 11, 2017 at 14:18
  • Hi CrazySabbath, thanks for the reply. I do have the Person class above showing the @OneToMany field in question. Would you also like me to show the getter and setters? (I only started getting this error when this @OneToMany field was added). Commented Jul 11, 2017 at 14:20
  • My bad, missed bolded text lol. Are you passsing entities directly to your controller? I would avoid that. Can you show Person class declaration? And @RequestBody print? If I were you, I would create other classes, with same fields and extending seriazable. Then I would convert those classes to JPA classes. Commented Jul 11, 2017 at 14:25
  • What's more, if you are using spring, you can add RestController annotations to your controller classes and remove @ResponseBody. Commented Jul 11, 2017 at 14:27
  • I have updated the code above to show the Person class definition. What do you mean by "And @RequestBody print? "? Also, in the Controller, I tried replacing @ResponseBody with @RestController, but I get: The annotation @RestController is disallowed for this location Commented Jul 11, 2017 at 14:30

1 Answer 1

1

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>();
Sign up to request clarification or add additional context in comments.

Comments

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.