@JsonIgnore and @JsonBackReference are being Ignored

北城余情 提交于 2019-11-30 09:48:34

use import com.fasterxml.jackson.annotation.JsonBackReference; instead of import org.codehaus.jackson.annotate.JsonBackReference;.

It was the problem for me.

The problem here seems to be related to using Set<User> instead of List<User>. I had exactly the same problem and changing from Set<User> to List<User> fixed this, otherwise I always got Infinite recursion error from Jackson. I don't know if this is really a bug in Jackson or do you have to provide some other annotations etc. when using Set.

Pedro Affonso

I had this problem and got the same code to work by updating the version of the Jackson library in my build (pom.xml) from 1.8.x to 1.9.13. If you are using maven, edit your pom.xml to contain:

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.13</version>
    </dependency>

Documentation doesn't help, but it seems that back references for Sets were not supported in the 1.8.x versions.

Look, I have something similar to you and is working just fine for me...

@JsonBackReference("promotion-travel")
@ManyToOne(cascade = CascadeType.ALL)
    @JoinTable(name="PROMOTION_TRAVEL_REL",
        joinColumns = @JoinColumn(name="TRAVEL_ID"),
        inverseJoinColumns = @JoinColumn(name="PROMOTION_ID")
)
public Promotion getPromotion(){...

And this is what I have on Entity1

@JsonManagedReference("promotion-travel")
@OneToMany(mappedBy="promotion")
public List<Travel> getTravelList(){...

I don't know if moving the annotation to the top will change anything, but that's the only thing that I can see...

Cheers,

i'm using spring 4.0.1 , hibernate 4.3.5 ,jackson 1.9.2 and STS IDE

i had the same exception but solved by annotating the setter by @Transient idont know why but it works fine

This is my entity classes User.class

    //i get that suggestion from some sites
    @JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
    @Entity
    @Table(name = "user", catalog = "someSchema")    
    public class User implements java.io.Serializable {

        private String name;
        private String password;
        private String username;
        private Set<Telephone> telephones = new HashSet<Telephone>(0);

        @OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
        public Set<Telephone> getTelephones() {
            return this.telephones;
        }

        public void setTelephones(Set<Telephone> telephones) {
            this.telephones = telephones;
        }
    }

Telephone.class

@Entity
@Table(name = "telephone", catalog = "someSchema")
public class Telephone implements java.io.Serializable {


    private User user;
    private String telephone;

    @ManyToOne(fetch = FetchType.LAZY)

    @JoinColumn(name = "user_id", nullable = false)
    public User getUser() {
        return this.user;
    }


    @Transient  
    public void setUser(User user) {
    this.user = user;
   }

}

concerning registering jackson to my application, i used xml config

   <mvc:annotation-driven>
        <mvc:message-converters>
            <bean
                class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="objectMapper">
                    <bean
                        class="web.jsonConverters.HibernateAwareObjectMapper" />
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

and mapper class

public class HibernateAwareObjectMapper extends ObjectMapper {

    public HibernateAwareObjectMapper() {
        Hibernate4Module hm = new Hibernate4Module();
        registerModule(hm);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!