Hibernate和Jackson(java.lang.IllegalStateException:在提交响应后无法调用sendError()) [英] Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed)
问题描述
您好Hibernate和Jackson有些问题。有两个POJO
Hello have some problem with Hibernate and Jackson. Have two POJOs
@Entity
@Table(name = "USERS")
public class User implements Serializable {
static final long serialVersionUID = 4235637833262722256L;
@Id
@GeneratedValue
private int id;
@Column(name = "CREATION_DATE")
private Date creationDate;
@Column(name = "FIRST_NAME")
private String firstName;
@Column(name = "LAST_NAME")
private String lastName;
@Version
@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;
@JsonManagedReference("role")
@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.
@Entity
@Table(name = "USER_ROLES")
public class UserRole {
@Version
@Column(name = "VERSION")
int version;
@Id
@GeneratedValue
private int id;
@JsonBackReference("role")
@ManyToOne()
@JoinColumn(name = "USER_ID", referencedColumnName = "ID")
private User user;
@Column(name = "ROLE")
private String role;
并且有一个控制器,通过杰克逊从用户Json制作。使用方法
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]
org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.doResolveException
Handling of [org.springframework.http.converter.HttpMessageNotWritableException]
resulted Exception java.lang.IllegalStateException: Cannot call sendError() after the response has been committed
当我添加@OneToMany(mappedBy =user,cascade = CascadeType.ALL)时出现问题,fetch = FetchType.EAGER),之前它只是fetch = FetchType.EAGER,但是我无法用他的角色保存用户,因为表USER_ROLE是空的。我在杰克逊的某处无限循环中解决了一些问题的解决方案,添加了一些anotation @JsonBackReference(role)和@JsonManagedReference(role)但没有任何改变。我怎么解决这个问题?谢谢!
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!
推荐答案
我建议在属性上添加 @jsonignore
导致循环引用。这将告诉杰克逊不要序列化该属性。
i recommend adding @jsonignore
on the property that is causing the circular reference. this will tell jackson not to serialize that property.
这篇关于Hibernate和Jackson(java.lang.IllegalStateException:在提交响应后无法调用sendError())的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!