JsonBackReference被忽略了

JsonBackReference被忽略了

本文介绍了@JsonIgnore和@JsonBackReference被忽略了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用RestEasy,Jboss 7和EJB 3.1。我正在创建一个以JSON格式返回数据的RESTful Web服务。

I'm working with RestEasy, Jboss 7 and EJB 3.1. I'm creating a RESTful web service that returns data in JSON format.

问题是我有一个 @ManyToOne 我的一个实体上的关系,它会在序列化过程中导致无限递归。我尝试使用Jackson的 @JsonIgnore @JsonBackReference 注释来解决问题但看起来似乎完全被忽略了无限递归仍在发生。

The problem is that I have a @ManyToOne relationship on one of my entities which causes an infinite recursion during serialization. I tried using Jackson's @JsonIgnore and @JsonBackReference annotations to fix the problem but it seems as if they are being totally ignored and the infinite recursion is still occurring.

这是我的用户类:

class User {
    private String userId;
    private Role role;

    @Id
    @Column(name = "\"UserId\"", unique = true, nullable = false, length = 30)
    public String getUserId() {
        return this.UserId;
    }

    public void setUserId(String UserId) {
        this.UserId = UserId;
    }

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "\"RoleId\"", nullable = false)
    //I have tried @JsonIgnore without @JsonBackReference
    @JsonIgnore
    //I have tried @JsonBackReference without @JsonIgnore
    //Also I have tried @JsonBackReference and @JsonIgnore together
    @JsonBackReference("role-User")
    public Role getRole() {
        return this.role;
    }
}

这是我的角色类的一部分:

This is a part of my Role Class:

@JsonManagedReference("role-User")
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "role")
public Set<User> getUsers() {
    return this.users;
}

我读过某个地方我应该用我的申请注册杰克逊才能够使用常规的Jaxb注释,所以我创建了一个类

I have read somewhere that I should register Jackson with my application to be able to use regular Jaxb annotation so I created a class

@Provider
@Produces(MediaType.APPLICATION_JSON)
public class JacksonContextResolver implements ContextResolver<ObjectMapper> {
    public JacksonContextResolver() {
        ObjectMapper mapper = new ObjectMapper();
        AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();

        mapper.getDeserializationConfig().setAnnotationIntrospector(
                introspector);
        mapper.getSerializationConfig().setAnnotationIntrospector(introspector);
    }

    public ObjectMapper getContext(Class<?> objectType) {
        return objectMapper;
    }
}

以上问题是不推荐使用JaxbAnnotationIntrospector()

请:


  • 你知道杰克逊注释被忽略的原因吗?

  • 我如何使用常规JAXB XML注释而不是Jackson?

  • 我可以使用什么代替 JaxbAnnotationIntrospector()

  • Do you have any idea why the Jackson annotations are being ignored?
  • How can I use the regular JAXB XML annotations instead of Jackson's?
  • What can I use instead of JaxbAnnotationIntrospector()?

答案感谢这些问题中的任何一个。谢谢。

An answer to any of these question is appreciated, thanks.

更新:

现在我已经使用 jboss-deployment-structure.xml 排除了 resteasy-jackson-provider ,而我正在使用Jettison。我仍然想知道如何使用Jackson!

For now I have excluded resteasy-jackson-provider using jboss-deployment-structure.xml and I am using Jettison instead. I still want to know how could I use Jackson!

推荐答案

使用 import com.fasterxml.jackson。 annotation.JsonBackReference; 而不是 import org.codehaus.jackson.annotate.JsonBackReference;

这对我来说是个问题。

这篇关于@JsonIgnore和@JsonBackReference被忽略了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 09:25