logo
down
shadow

JsonIgnore and JsonBackReference are being Ignored


JsonIgnore and JsonBackReference are being Ignored

By : user2948196
Date : November 14 2020, 04:48 PM
I think the issue was by ths following , i found a way by annotating the setter by @Transient
idont know why but it works fine
code :
    //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;
        }
    }
   @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;
      }

    }
    @RestController
    @RequestMapping("/user") 
    public class UserController extends ParentController {

        static final Logger logger = Logger.getLogger(UserController.class.getName());
        @Autowired
        IUserDao iuserdao;

            @RequestMapping(value = "/signin", method = RequestMethod.POST)
            public ResponseEntity<User> signin(@RequestBody LoginWrapper login) {
                System.out.println("==============GET USER==============");
                try {
                    User user = iuserdao.signin(login);
                    if (user == null) {
                        HttpHeaders httpHeaders = new HttpHeaders();
                        httpHeaders.set(ERR_HEADER_NAME, "user not exist");
                        return new ResponseEntity<User>(httpHeaders, HttpStatus.NOT_FOUND);
                    } else {
                        List<Telephone> tels=user.getTelephones();
                        for (Telephone telephone : tels) {
                            telephone.setUser(null);
                        }
                        return new ResponseEntity<User>(user, HttpStatus.OK);
                    }

                } catch (Exception e) {
                    System.out.println(e.getMessage());
                    return null;
                }

            }


Share : facebook icon twitter icon
JsonManagedReference vs JsonBackReference

JsonManagedReference vs JsonBackReference


By : user3928549
Date : March 29 2020, 07:55 AM
should help you out
@JsonManagedReference is the forward part of reference – the one that gets serialized normally. @JsonBackReference is the back part of reference – it will be omitted from serialization.
code :
public class User {
    public int id;
    public String name;

    @JsonBackReference
    public List<Item> userItems; 
} 

public class Item {
    public int id;
    public String itemName;

    @JsonManagedReference
    public User owner; 
 }
Difference between @JsonIgnore and @JsonBackReference, @JsonManagedReference

Difference between @JsonIgnore and @JsonBackReference, @JsonManagedReference


By : Emmanuel Bolos
Date : March 29 2020, 07:55 AM
Hope this helps I know both @JsonIgnore and @JsonManagedReference, @JsonBackReference are used to solve the Infinite recursion (StackOverflowError), what is the difference between these two? , Lets suppose we have
code :
private class Player {
    public int id;
    public Info info;
}
private class Info {
    public int id;
    public Player parentPlayer;
}

// something like this:
Player player = new Player(1);
player.info = new Info(1, player);
private class Info {
    public int id;
    @JsonIgnore
    public Player parentPlayer;
}
private class Player {
    public int id;
    @JsonManagedReference
    public Info info;
}

private class Info {
    public int id;
    @JsonBackReference
    public Player parentPlayer;
}
Is there any way to use JsonManagedReference and JsonBackReference in both directions?

Is there any way to use JsonManagedReference and JsonBackReference in both directions?


By : Ellie
Date : March 29 2020, 07:55 AM
should help you out After a lot of tries, implemented the traversing bi-directional with @JsonIgnoreProperties
example: "@JsonIgnoreProperties(allowSetters = true, value = { "enterprise" })"
@JsonIgnore and @JsonBackReference are being Ignored

@JsonIgnore and @JsonBackReference are being Ignored


By : leo gigi
Date : March 29 2020, 07:55 AM
Does that help use import com.fasterxml.jackson.annotation.JsonBackReference; instead of import org.codehaus.jackson.annotate.JsonBackReference;.
It was the problem for me.
JsonIgnore on Field vs JsonIgnore on getter of a field in Jackson

JsonIgnore on Field vs JsonIgnore on getter of a field in Jackson


By : Ravi Leal
Date : March 29 2020, 07:55 AM
This might help you @JsonIgnore annotation is used to ignore fields from de-serialization and serialization, it can be put directly on the instance member or on its getter or its setter. The application of the annotation in any of these 3 points, leads to the total exclusion of the property from both the serialization and de-serialization processes (and this applies starting from Jackson 1.9; the version used in these examples is Jackson 2.4.3).
Note: Before version 1.9, this annotation worked purely on method-by-method (or field-by-field) basis; annotation on one method or field did not imply ignoring other methods or fields
code :
 import java.io.IOException;

 import com.fasterxml.jackson.annotation.JsonIgnore;
 import com.fasterxml.jackson.core.JsonParseException;
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.JsonMappingException;
 import com.fasterxml.jackson.databind.ObjectMapper;

 class MyTestClass {

 private long id;
 private String name;
 private String notInterstingMember;
 private int anotherMember;
 private int forgetThisField;

 public long getId() {
    return this.id;
 }

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

 public String getName() {
     return this.name;
 }

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

 @JsonIgnore
 public String getNotInterstingMember() {
    return this.notInterstingMember;
 }

 public void setNotInterstingMember(String notInterstingMember) {
    this.notInterstingMember = notInterstingMember;
 }

 public int getAnotherMember() {
    return this.anotherMember;
 }

 public void setAnotherMember(int anotherMember) {
    this.anotherMember = anotherMember;
 }

 public int getForgetThisField() {
    return this.forgetThisField;
 }

 @JsonIgnore
 public void setForgetThisField(int forgetThisField) {
    this.forgetThisField = forgetThisField;
 }

 @Override
 public String toString() {
    return "MyTestClass [" + this.id + " , " +  this.name + ", " + this.notInterstingMember + ", " + this.anotherMember + ", " + this.forgetThisField + "]";
    }

  }
 {"id":1,"name":"Test program","anotherMember":100}
 MyTestClass [1 , Test program, null, 100, 0]
Related Posts Related Posts :
  • Convert JSON to CSV - string manipulation (jq, bash, awk, sed, etc.)
  • Is there a bug in the new Spring JSON reader or am I doing something wrong?
  • Is it possible to skip or ignore a attribute while using Streaming API of Jackson
  • Json Object Deserialization
  • jsonSlurper: get one value based on other value
  • How to break a JSON response into smaller JSON part of a particular batch size
  • How do I offer JSON as a url with parameters?
  • Getting Error While creating DOJOX enchanced grid programmatically
  • Decode Feedly JSON data
  • Not in scope: `HashMap.lookup'
  • Reddit data scraping with json, 'after' not making a difference in '.json?limit=<limit>&after=<id>'
  • Symfony 2.6 - Flot chart doesn't populate properly from JSON
  • It is possible to display geojson as table in ng-repeat loop in Angular?
  • convert system.Object[] to Datatable in VB.NET
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org