我在前端使用[[${user}]]
获取了 controller中map.put("user",user)的值,出现了这个错误,我的user和另外一个类存在 manytoone的关系,
因为thymeleaf序列化时并不是使用的jackson所以使用@jsonignore对于thymeleaf来说是无效的,
所以我的解决办法是 自己手动 使用jackson解析user对象,这样user对象上的 @jsonignore 注解就可以生效了;
@RequestMapping("") public String hello(ModelMap map) { User user=urep.getOne(1); ObjectMapper mapper = new ObjectMapper(); String json = ""; try { json = mapper.writeValueAsString(user); } catch (JsonProcessingException e) { e.printStackTrace(); } map.put("user",json);
前端:
user:JSON.parse([[${user}]])
springboot version :1.5.9.RELEASE
My English is very poor, sorry, so I used Google translation.
//我英语很差,抱歉,所以我用了谷歌翻译
I use the spring data jpa, my two entities exist mutual references, so the following error, how can I solve?
//我使用了spring data jpa,我的两个 entity 存在互相引用的情况,所以出现了下方的错误,请问我该如何解决?
Why did I use @ JsonIgnore did not work it?
//为什么我使用了 @jsonignore 却没有起作用呢
@entity
@jsonignoreproperties({"hibernateLazyInitializer", "handler"})
public class User {
@id
@generatedvalue
Integer id;
String name;
@onetomany(cascade = CascadeType.REFRESH)
@joincolumn(name = "user_id")
@jsonignore
Set shuos;
@JsonIgnore
public Set<Shuo> getShuos() {
return shuos;
}
@JsonIgnore
public void setShuos(Set<Shuo> shuos) {
this.shuos = shuos;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@entity
@jsonignoreproperties({"hibernateLazyInitializer", "handler"})
public class Shuo {
@id
@generatedvalue
int id;
String content;
Date date;
@ManyToOne(cascade = CascadeType.REFRESH)
@JoinColumn(name = "user_id")//加入一列作为外键
@JsonIgnore
User user;
@JsonIgnore
public User getUser() {
return user;
}
@JsonIgnore
public void setUser(User user) {
this.user = user;
}
@ManyToMany(cascade = CascadeType.REFRESH)
Set<Tag> tags;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Set<Tag> getTags() {
return tags;
}
public void setTags(Set<Tag> tags) {
this.tags = tags;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Shuo() {
}
public Shuo(String content, Date date) {
this.content = content;
this.date = date;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
Front-end code //前端代码:
if( [[${user}]] is exist ){
var app = new Vue({
el: '#app',
data: {
user:[[${user}]] If there is this code, it will be wrong.//如果这段代码存在就会报错
},
java.lang.StackOverflowError: null
at sun.reflect.misc.ReflectUtil.checkPackageAccess(ReflectUtil.java:164) ~[na:1.8.0_151]
at sun.reflect.misc.ReflectUtil.isPackageAccessible(ReflectUtil.java:195) ~[na:1.8.0_151]
at java.beans.Introspector.getBeanInfo(Introspector.java:164) ~[na:1.8.0_151]
at org.thymeleaf.util.JavaScriptUtils.printObject(JavaScriptUtils.java:353) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.thymeleaf.util.JavaScriptUtils.print(JavaScriptUtils.java:184) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.thymeleaf.util.JavaScriptUtils.printKeyValue(JavaScriptUtils.java:346) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.thymeleaf.util.JavaScriptUtils.printMap(JavaScriptUtils.java:337) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.thymeleaf.util.JavaScriptUtils.printObject(JavaScriptUtils.java:365) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.thymeleaf.util.JavaScriptUtils.print(JavaScriptUtils.java:184) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.thymeleaf.util.JavaScriptUtils.printKeyValue(JavaScriptUtils.java:346) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.thymeleaf.util.JavaScriptUtils.printMap(JavaScriptUtils.java:337) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.thymeleaf.util.JavaScriptUtils.printObject(JavaScriptUtils.java:365) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE]
} else {
var app = new Vue({
el: '#app',
data: {
user:''
},
Everything is all right //一切正常
}