Jackson的Stakover流错误应用于JPA实体以生成JSON [英] Stakover flow error with Jackson applied on JPA Entities to generate JSON
问题描述
我有一个具有OneToMany
关系的JPA
代码. Customer
具有要检出的Item
列表.但是,代码继续生成StackOverflowError
.
I have a JPA
code with OneToMany
relationship. A Customer
has a list of Item
to check out. However, the code continue to generate StackOverflowError
.
一次,我从客户实体获取List<Item>
时通过应用@JsonIgnore
解决了这一问题.但这似乎不再起作用.
Once, I had resolved this one by applying @JsonIgnore
while fetching the List<Item>
from Customer entity. But even that does not seem to work anymore.
在Customer
类中:
@OneToMany(mappedBy = "customer", orphanRemoval = true)
@JsonIgnore
private List<Item> items;
在Item
类中:
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "CUSTOMER_ID", nullable = false)
private Customer customer;
和CustomerRest
类:
@Path("customers")
public class CustomerRest {
@Inject
NewSessionBean newSessionBean;
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Customer> getAllCustomers() {
return newSessionBean.getCustomers();
}
}
方法newSessionBean.getCustomers()
:
public List<Customer> getCustomers(){
TypedQuery<Customer> q= em.createQuery("select c from Customer c", Customer.class);
return q.getResultList();
}
我期望格式正确的JSON消息,但是没有任何迹象.我得到的只是浏览器上的java.lang.StackOverflowError
,服务器日志生成以下内容:
I expect a nicely formatted JSON message but there is no sign of this. All I get is the java.lang.StackOverflowError
on the browser and the Server log generates the following:
Generating incomplete JSON|#]
java.lang.StackOverflowError
java.lang.StackOverflowError at org.eclipse.yasson.internal.serializer.DefaultSerializers.findByCondition(DefaultSerializers.java:130)
推荐答案
您似乎在使用 Yasson 项目不是杰克逊.在这种情况下,您应该使用@JsonbTransient
批注.请参见文档:
It looks like you use Yasson project not Jackson. In that case you should use @JsonbTransient
annotation. See documentation:
默认情况下,
JSONB
会忽略具有非公共访问权限的属性.全部 公共财产-具有以下内容的公共领域或非公共领域 公共获取者被序列化为JSON
文本.
By default,
JSONB
ignores properties with a non public access. All public properties - either public fields or non public fields with public getters are serialized intoJSON
text.
可以使用@JsonbTransient
批注来排除属性.
用@JsonbTransient
注释注释的类属性将被忽略
通过JSON Binding
引擎.行为因位置而异
放置@JsonbTransient
批注.
Excluding properties can be done with a @JsonbTransient
annotation.
Class properties annotated with @JsonbTransient
annotation are ignored
by JSON Binding
engine. The behavior is different depending on where
@JsonbTransient
annotation is placed.
另请参阅:
这篇关于Jackson的Stakover流错误应用于JPA实体以生成JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!