← All posts

Spring Boot: Why Your REST API Keeps Returning Infinite Nested Objects (And How to Fix It)

Spring Boot: Why Your REST API Keeps Returning Infinite Nested Objects (And How to Fix It)
DateDecember 7, 2026TagsSpring JPASpringboot

Spring Boot: Why Your REST API Keeps Returning Infinite Nested Objects (And How to Fix It)

I was testing one of my Spring Boot REST APIs using Postman when I noticed something strange.

The response just kept going.

Customer → Orders → Customer → Orders → Customer → Orders...

At first, it looked like something was wrong with the endpoint itself.

But the API wasn't crashing. There were no obvious exceptions. The response was simply getting bigger and bigger, repeating the same objects over and over again.

And that's actually worse when you're trying to build a clean REST API.

What was causing the problem?

The issue came from a bidirectional relationship between two JPA entities:

A customer can have multiple orders, and each order belongs to a customer.

So naturally, the entities reference each other.

For example:

Customer.java

@Entity
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    @OneToMany(mappedBy = "customer")
    private List<CustomerOrder> orders;
}

CustomerOrder>java

@Entity
public class CustomerOrder {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String product;

    @ManyToOne
    @JoinColumn(name = "customer_id")
    private Customer customer;
}

From a database perspective, this relationship makes perfect sense.

But here's the problem.

When your controller returns a Customer object, Jackson tries to convert it into JSON.

It sees the customer's orders.

Then it sees that each order references its customer.

So it tries to serialize the customer again.

And then its orders again.

You end up with an infinite serialization cycle.

The fix: @JsonManagedReference and @JsonBackReference

Fortunately, the solution is pretty straightforward.

Jackson provides two annotations that help manage bidirectional relationships during JSON serialization.

1. @JsonManagedReference

Place this annotation on the parent side of the relationship.

@OneToMany(mappedBy = "customer")
@JsonManagedReference
private List<CustomerOrder> orders;

This tells Jackson to serialize this side of the relationship normally.

2. @JsonBackReference

Place this annotation on the child side.

@ManyToOne
@JoinColumn(name = "customer_id")
@JsonBackReference
private Customer customer;

This tells Jackson to skip this back-reference during serialization.

Now, when you request a customer, the response can look something like this:

{
  "id": 1,
  "name": "John",
  "orders": [
    {
      "id": 101,
      "product": "Laptop"
    },
    {
      "id": 102,
      "product": "Keyboard"
    }
  ]
}

No more endless nesting.

The customer is serialized, its orders are included, but each order doesn't serialize the customer again.

Why does this actually work?

The important thing to understand is that the two annotations have different responsibilities.

They work together to break the recursive serialization cycle.

One important detail: @JsonBackReference also affects deserialization. The back-reference is not populated from JSON in the same way as a normal property.

If you have more complex relationships or multiple bidirectional associations, you also need to consider naming reference pairs appropriately.

Is this the best solution for every project?

Not necessarily.

For simple cases, these annotations are useful and get the job done.

But in larger applications, I would seriously consider using DTOs (Data Transfer Objects).

Why?

Because your JPA entities represent your database relationships, while your API responses should represent the data your clients actually need.

Those aren't always the same thing.

DTOs give you more control over:

Final thoughts

This was a good reminder that just because two entities reference each other in JPA doesn't mean you should serialize both directions in your REST API.

Sometimes the problem isn't your database, your repository, or your controller.

It's simply how your objects are being converted into JSON.

Have you run into this issue with bidirectional JPA relationships?

Do you usually solve it with Jackson annotations, or do you prefer DTOs from the beginning?

I'd be interested to hear how other Spring Boot developers handle this in their projects.