Home » Java » Enterprise Java » JSON – Jackson to the rescue

About Michal Jastak

Michal Jastak
Michał is a Chief Technology Officer in Java Division of AIS.PL, company developing mostly Web Applications of different kind, usually e-Government related.

JSON – Jackson to the rescue

Sometimes you have to fetch some data from the server in JavaScript, JSON is pretty good choice for this task.

Let’s play with the Employer – Employee – Benefit example from the post JPA Demystified (episode 1) – @OneToMany and @ManyToOne mappings. We will use it inside the web application based on Spring Framework.

Our first controller will return the employees list as the response body, in our case MappingJacksonHttpMessageConverter will be used automagically for converting the value returned by handleGet method to the response send to client.


public class EmployeeListController {
    private EmployerDAO employerDAO;

    @RequestMapping(method = RequestMethod.GET)
    public List<Employee> handleGet(@RequestParam('employerId') Long employerId) {
        return employerDAO.getEmployees(employerId);

When we try to fetch the data for the first time, we encounter beautiful exception: JsonMappingException: Infinite recursion (StackOverflowError) – caused by bi-directional references between the Employer – Employee – Benefit.

Looking for the possible solution, I’ve found a note Handle bi-directional references using declarative method(s), and after reading it, I’ve corrected the domain entities in following way:

@Table(name = 'EMPLOYERS')
public class Employer implements Serializable {
    @OneToMany(mappedBy = 'employer', cascade = CascadeType.PERSIST)
    public List

                       getEmployees() {
        return employees;

@Table(name = 'EMPLOYEES')
public class Employee implements Serializable {
    @OneToMany(mappedBy = 'employee', cascade = CascadeType.PERSIST)
    public List

                        getBenefits() {
        return benefits;

    @ManyToOne(optional = false)
    @JoinColumn(name = 'EMPLOYER_ID')
    public Employer getEmployer() {
        return employer;

@Table(name = 'BENEFITS')
public class Benefit implements Serializable {
    @ManyToOne(optional = false)
    @JoinColumn(name = 'EMPLOYEE_ID')
    public Employee getEmployee() {
        return employee;

After performing the above changes, I could finally enjoy the JSON response returned by my code:

[{'id':1, 'benefits':[{'name':'Healthy Employees', 'id':1, 'type':'HEALTH_COVERAGE', 'startDate':1104534000000, 'endDate':null}, {'name':'Gold Autumn','id':2,'type':'RETIREMENT_PLAN','startDate':1104534000000,'endDate':null},{'name':'Always Secured','id':3,'type':'GROUP_TERM_LIFE','startDate':1104534000000,'endDate':null}],'firstName':'John'},{'id':2,'benefits':[],'firstName':'Mary'},{'id':3,'benefits':[],'firstName':'Eugene'}]

And as usual some links for the dessert:

Reference: JSON – Jackson to the rescue from our JCG partner Micha? Ja?tak at the Warlock’s Thoughts blog.

(0 rating, 0 votes)
You need to be a registered member to rate this.
1 Comment Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Leave a Reply

1 Comment threads
0 Thread replies
Most reacted comment
Hottest comment thread
1 Comment authors
Blaise Doughan Recent comment authors

This site uses Akismet to reduce spam. Learn how your comment data is processed.

newest oldest most voted
Notify of
Blaise Doughan
Blaise Doughan

FYI – EclipseLink JAXB (MOXy) also supports bidirectional relationships in its JSON-binding through our inverse reference mapping. As EclipseLink is also a JPA provider it is a use case we’re very familiar with:

MOXy’s @XmlInverseReference is now Truly Bidirectional