Using Jackson to JSON to resolve bidirectional associative loop calls

Source: Internet
Author: User

Itoo V1.0 development is considered to be over, and now is the summary of the handover link, in this project common problems should be well organized and share with you, this time the main introduction turn JSON the problem of looping calls.

I. Background of the problem

believe as long as the use ORM mapping entity affinity relationships, where there are two-way associations in entities that have encountered such problems:


In fact, this problem was last . NET project, so this is a common problem, just because this is really Java The first time, but the principle of cyclic invocation is still unchanged.

Second, the solution

because just Java project experience is not rich, before the entity relationship is not complex in this project, so there is no solution, but learn to stand on the shoulders of giants or a good way to solve problems, in fact, there are many solutions to this problem, here I chose a better:

1 , first we need to introduce Jackson of the Jar Package:

Https://cn.jarfire.org/jackson.all.html

Available for download here.

2 , Package turn JSON the tool class:

Package Itoo.basic.tool.json;import Java.io.printwriter;import Javax.servlet.http.httpservletresponse;import Org.codehaus.jackson.jsongenerator;import org.codehaus.jackson.map.objectmapper;/** * Tool class for converting JSON  * @author Chenlina * @version 1.0.0, December 23, 2014 afternoon 3:24:33 */public class Jacksonjsonuntil{public Jsongenerator jsongenerator=null;public Objectmapper objectmapper=null;/** * Convert an object to a JSON string * @param format when data is passed to the foreground * @param obj object to convert (can be list) * @throws E Xception exception handling */public void Beantojson (HttpServletResponse response,object obj) throws exception{ Response.setcontenttype ("Application/json;charset=utf-8");//Prevent data transmission garbled Objectmapper =new objectmapper (); try {String Json=objectmapper.writevalueasstring (obj);//Convert object to Jsonprintwriter out=response.getwriter ()//Print to foreground out.write (JSON );} catch (Exception e) {//Todo:handle exceptione.printstacktrace ();}}}


3 , add annotations to entities with many-to-many relationships:

Instead of taking a single entity as an example, the annotations used are:

( 1 ) Exclude Attributes

@JsonIgnore, generally marked on a property or method, acting on serialization and deserialization;

@JsonIgnoreProperties, if it is a proxy class, because it cannot be marked on a property or method, it can be marked on the class declaration, and also used for deserialization when the field is resolved;

There is also when using Hibernate, the entity class generated after querying the database is a proxy class, when the conversion JSON will error;

There are two ways to resolve this:

1) Set the Fail_on_empty_beans property tells the Jackson empty object not to throw an exception;

Mapper.disable (SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS);

2) Use @JsonIgnoreProperties Annotations

at the entity class declaration, add @JsonIgnoreProperties (value = {"Hibernatelazyinitializer", "handler"}) annotations;

Recommended Use @JsonIgnoreProperties Annotations so that no extra fields are generated in the generated JSON;

Iii. Summary

Jackson solve the problem of circular call, just avoid the circular call, rather than really fundamentally solve, hope to solve this problem later.

Using Jackson to JSON to resolve bidirectional associative loop calls

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.