Could not write JSON: Infinite recursion (StackOverflowError)

Return data format json

        

        
Json data return to unity in the controller's conversion, there have been json infinite recursion stackoverflowerror errors that converts json object in json format data, there have been cases infinite recursive call.

        
Details are as follows:

        
HmAppProductMgEntity class has attributes: private List <HmMoneyInRecEntity> hmMoneyInRecEntityList ;,

        
HmAppProductMgEntity relationship with HmMoneyInRecEntity is OneToMany;

        
In HmMoneyInRecEntity class has attributes private HmAppProductMgEntity hmAppProductEntity

        
Id HmAppProductMgEntity reference to the field, and as a foreign key.

        
hibernate query result is normal, you can see HmAppProductMgEntity object returned, there HmMoneyInRecEntity parameter values, but when json conversion situation appeared infinite recursion. Personal analysis, should be the json when serialized hmMoneyInRecEntityList property HmAppProductMgEntity in, and found HmMoneyInRecEntity class, and then serialize HmMoneyInRecEntity class, HmMoneyInRecEntity class has hmAppProductEntity property, therefore, in order to serialize hmAppProductEntity property, json had to go to serialize A class, so recursion repeatedly, causing the problem.

        
solve:

        
In HmMoneyInRecEntity class annotated getter method hmAppProductEntity @JsonBackReference on, so the problem is solved.

        
@JsonBackReference and @JsonManagedReference: paired two generally denoted generally by the parent-child relationship. @JsonBackReference label attributes serialization (serialization, coming object into json data), it is ignored (i.e. json result data does not contain the contents of the attribute). @JsonManagedReference label attribute will be serialized. When serialization @ JsonBackReference effect equivalent @JsonIgnore, at this time can not @JsonManagedReference. However, deserialization (deserialization, i.e. json data into an object), if not @JsonManagedReference, automatically injected property is not marked @JsonBackReference (parent or child is ignored); if @JsonManagedReference, is automatically injected @JsonBackReference label attribute automatic injection.
@JsonIgnore: simply ignore a property to disconnect infinite recursion, serialization or deserialization are ignored. Of course, if marked get, set method, can be separately controlled, corresponds to the sequence of the get method, deserialization corresponding method is set. In the parent-child relationship, when deserialized, @ JsonIgnore not automatically inject property value is ignored (parent or child), this is it with @JsonBackReference and @JsonManagedReference biggest difference.

        
Transfer from: http: //blog.csdn.net/ludengji/article/details/11584281

Guess you like

Origin www.cnblogs.com/gaoxiaowei199110/p/12167176.html