Jackson的Stakover流错误应用于JPA实体以生成JSON [英] Stakover flow error with Jackson applied on JPA Entities to generate JSON

查看:176
本文介绍了Jackson的Stakover流错误应用于JPA实体以生成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 into JSON 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆