1

@JsonIgnore annotation not working and i am not using Spring Framework here is my code below:

import Plat.Hibernate.Util.DataBaseObject;
import org.codehaus.jackson.annotate.*;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
public class Parent implements DataBaseObject{
    private int id;
    private String name;
    @JsonIgnore
    private List<Child> child;

    public Parent(){
        child = new ArrayList<>();
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Basic
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @OneToMany(mappedBy = "parent",cascade = CascadeType.ALL,fetch = FetchType.LAZY)
    @JsonIgnore
    public List<Child> getChild() {
        return child;
    }

    @JsonIgnore
    public void setChild(List<Child> child) {
        this.child = child;
    }
}

and the other class:

import org.codehaus.jackson.annotate.*;
import javax.persistence.*;

@Entity
public class Child implements DataBaseObject{
    private int id;
    private String name;
    @JsonIgnore
    private Parent parent;

    public Child() {
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Basic
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
    @JoinColumn(name = "parent_id")
    @JsonIgnore
    public Parent getParent() {
        return parent;
    }

    @JsonIgnore
    public void setParent(Parent parent) {
        this.parent = parent;
    }
}

and i am using Maven building tool in my project so this is the dependency for the @JsonIgnore annotation:

<dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-lgpl</artifactId>
        <version>1.9.13</version>
    </dependency>

The problem is that when i use REST API request on this entity i get infinite loop (error 500 Internal Error) so i need to use the @JsonIgnore annotation to avoid this loop and it is not working

  • Please read [How do I ask a good question](http://stackoverflow.com/help/how-to-ask). You don't ask any question. And you talk about "not working". Better describe what you expect to happen and what happens instead. Provide any error message. – Codo Jan 02 '17 at 12:39
  • Duplicate of Thread : [Spring @JsonIgnore not working](http://stackoverflow.com/questions/19894955/spring-jsonignore-not-working?rq=1), Please try the solutions mentioned there. I think this can help. – Rohit Gulati Jan 02 '17 at 12:39
  • Rohit Gulati i tried this solution and it didn't work for me , as you see in my code i change the packages as proposed in the previous solution – Montaser Qasem Jan 02 '17 at 12:50
  • Codo- i have updated my question, please read it again. – Montaser Qasem Jan 02 '17 at 12:53
  • It's still difficult to analyze the problem. Can you provide the stacktrace? And can you add the code the generates the JSON data structure? And I propose you remove the *hibernate* tag as the question is not related to Hibernate. – Codo Jan 02 '17 at 14:12
  • I just told you the problem, you don't have to analyze it! – Montaser Qasem Jan 02 '17 at 16:13
  • Take a look at org.codehaus.jackson.annotate vs com.fasterxml.jackson.annotation.JsonIgnore May be the problem – Jonathas Pacífico Jun 02 '17 at 14:05

0 Answers0