Jackson 的 Stakover 流错误应用于 JPA 实体以生成 JSON

Stakover flow error with Jackson applied on JPA Entities to generate JSON

我有一个 JPA 代码与 OneToMany 关系。 Customer 有一个 Item 的列表要签出。但是,代码继续生成 WhosebugError

有一次,我通过在从 Customer 实体获取 List<Item> 时应用 @JsonIgnore 解决了这个问题。但即使这样似乎也不再有效了。

Customer class:

@OneToMany(mappedBy = "customer", orphanRemoval = true)
@JsonIgnore
private List<Item> items;

Item class:

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "CUSTOMER_ID", nullable = false)
private Customer customer;

CustomerRest class:

@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.WhosebugError 和服务器日志生成以下内容:

Generating incomplete JSON|#]
    java.lang.WhosebugError
    java.lang.WhosebugError    at org.eclipse.yasson.internal.serializer.DefaultSerializers.findByCondition(DefaultSerializers.java:130)

你好像用了Yasson project not Jackson. In that case you should use @JsonbTransient annotation. See documentation:

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.

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.

另请参阅: