2

In short

I need to find the root cause of the error messages which is displayed in the spring data rest. How to find the exact place where I should put @JsonIgnore, or @RestResource(exported = false) by looking at the generic error message?

In Detail

I embed my Spring Application with spring data rest. Though there are many entities and repositories, I only wanted few entities to get exposed via rest. One of the entity is user.

But I get the following error message. How can I drill down to the place where I can actually find the root cause of this generic error message?

Could not write content: Infinite recursion (StackOverflowError) (through reference chain: org.springframework.hateoas.Resources["_embedded"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: org.springframework.hateoas.Resources["_embedded"])

Repository

@RepositoryRestResource(collectionResourceRel = "system-users", itemResourceRel = "system-user", path = "system-users")
public interface UserRepository extends CrudRepository<User, Long>, DatatablesCriteriasRepository<User>{

    @RestResource(exported = false)
    @Query("SELECT u FROM User u INNER JOIN u.roles role WHERE role.role in :roles")
    Iterable<User> findByRoles(@Param("roles") String... roles);

    @RestResource(exported = false)
    @Query("SELECT u FROM User u WHERE u.username = :username")
    User findByUsername(@Param("username") String username);
    ...
    ...
}

Model

@Entity
@Table(name = "system_user_tab")
public class User{
...
...

I know by using @JsonIgnore, @RestResource(exported = false) I can try to eliminate the issue. But I need to know how I can understand the exact place where I have to use these annotations, by looking at this error message.

Faraj Farook
  • 14,385
  • 16
  • 71
  • 97
  • possible duplicate of [Infinite Recursion with Jackson JSON and Hibernate JPA issue](http://stackoverflow.com/questions/3325387/infinite-recursion-with-jackson-json-and-hibernate-jpa-issue) – Sarit Adhikari Jun 06 '15 at 12:30
  • @SaritAdhikari, not really, in this question I'm asking how to find the root cause of such generic error messages. – Faraj Farook Jun 06 '15 at 12:32
  • So how to understand where I should put the json ignoring annotation, by looking at this given generic error message? – Faraj Farook Jun 06 '15 at 12:36
  • I don't think we can dig in any further for such error . In this case look for bi-directional references in your model class using ORM. Mostly it will be the portion using many-to-many annotation. – Sarit Adhikari Jun 06 '15 at 12:41
  • Thank you for the comment. But the issue is originating from `org.springframework.hateoas.Resources["_embedded"])` if i directly print out the user object from a rest controller, it works fine. So can this be a cause of bi-directional ORM reference? FYI, I commented the relationships of user model with the json ignoreing annotations. – Faraj Farook Jun 06 '15 at 12:50
  • I ended up debugging my problem by putting a breakpoint at `LinkCollectingAssociationHandler.doWithAssociations:101` and just stepped through to figure out which entities were giving me trouble. (This was with Spring Data REST version 2.2.3.RELEASE, but doesn't look like that source has changed too much) – dardo Jan 25 '16 at 21:08

1 Answers1

1

You can do it in newer version of Spring Data Rest 2.4.0.RELEASE:

Could not write content: Infinite recursion (StackOverflowError) (through reference chain: org.springframework.hateoas.PagedResources[\"_embedded\"]->java.util.UnmodifiableMap[\"payment\"]->java.util.ArrayList[0]->org.springframework.data.rest.webmvc.json.ProjectionResource[\"content\"]->$Proxy153[\"subject\"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: org.springframework.hateoas.PagedResources[\"_embedded\"]->java.util.UnmodifiableMap[\"payment\"]->java.util.ArrayList[0]->org.springframework.data.rest.webmvc.json.ProjectionResource[\"content\"]->$Proxy153[\

It will be field subject.

Artur M.
  • 318
  • 4
  • 8