Aleksander Melnichnikov Aleksander Melnichnikov - 1 month ago 16x
Java Question

Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed)

Hello have some problem with Hibernate and Jackson. Have two POJOs

@Table(name = "USERS")
public class User implements Serializable {
static final long serialVersionUID = 4235637833262722256L;

private int id;

@Column(name = "CREATION_DATE")
private Date creationDate;

@Column(name = "FIRST_NAME")
private String firstName;

@Column(name = "LAST_NAME")
private String lastName;

@Column(name = "VERSION")
private int version;

@Column(name = "E_MAIL")
private String email;

@Column(name = "ENABLED")
private boolean enabled;

@Column(name = "LOGIN")
private String login;

@Column(name = "PASSWORD")
private String password;

@OneToMany(mappedBy = "user", cascade = CascadeType.ALL,fetch = FetchType.EAGER)
private Set<UserRole> userRole;

User's and his Role that's made for spring security integration.

@Table(name = "USER_ROLES")
public class UserRole {

@Column(name = "VERSION")
int version;
private int id;

@JoinColumn(name = "USER_ID", referencedColumnName = "ID")
private User user;

@Column(name = "ROLE")
private String role;

And have a controller which make from User Json via Jackson. With method

@RequestMapping(value = "/login", method = RequestMethod.GET, produces = "application/json")
private @ResponseBody User checkLoginAvailability(@RequestParam String login) {
User user = appContext.getUserService().readUserByLogin(login);
if (user != null) {
return user;
return new User();

When this method return user without null fields it throws that:

26-Oct-2014 14:31:17.652 WARNING [http-nio-8080-exec-4]
Handling of [org.springframework.http.converter.HttpMessageNotWritableException]
resulted Exception java.lang.IllegalStateException: Cannot call sendError() after the response has been committed

That's problem came when i added @OneToMany(mappedBy = "user", cascade = CascadeType.ALL,fetch = FetchType.EAGER), previously it was just fetch = FetchType.EAGER, but i wasn't able to save User with his role in this case the table USER_ROLE was empty. I've red some solution that problem in infinite loop somewhere in Jackson, added some anotation @JsonBackReference("role") and @JsonManagedReference("role") but nothing changed. How can i solve this problem? Thank you!


i recommend adding @jsonignore on the property that is causing the circular reference. this will tell jackson not to serialize that property.