0

I am building a Spring repository for some JPA-annotated entities. I have created a repository:

public interface AppRepository extends PagingAndSortingRepository<App, String>
{
}

The App class looks as follows:

@Entity
public class App implements Serializable
{
    @Id
    private String appId;
    @OneToMany(mappedBy = "app")
    private List<AgentUser> agentusers;
    @OneToMany(mappedBy = "app")
    private List<AppFacet> appfacets;
    // getters and setters go here
}

where the AgentUser and the AppFacet hold a reference property called app towards an App object. In the AgentUser class, I have changed the RestResource rel:

@Entity
public class AgentUser
{
    ...
    @ManyToOne
    @JoinColumn(name = "AppId")
    @RestResource(rel = "agentUserToApp", exported = false)
    private App app;
    // other properties go here
}

I am getting the following error message while querying the /apps path:

org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: Infinite recursion (StackOverflowError) (through reference chain: org.springframework.hateoas.PagedResources["_embedded"]);

Do you know what could be causing it? Please note that I only have one App object in a database, for testing purposes and no other kind of object.

Update

The trace is:

com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:677)
com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:156)
com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:129)
com.fasterxml.jackson.databind.ObjectMapper.writeValue(ObjectMapper.java:2240)
org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:231)
org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:208)
org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:161)
org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:101)
org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:167)

And after that, a lot of:

org.springframework.data.mapping.model.BasicPersistentEntity.doWithAssociations(BasicPersistentEntity.java:352)
org.springframework.data.rest.webmvc.mapping.LinkCollectingAssociationHandler.doWithAssociation(LinkCollectingAssociationHandler.java:101)
2
  • Add the complete stacktrace! Commented Jun 19, 2015 at 17:03
  • 1
    You have "Infinite recursion", read: stackoverflow.com/questions/3325387/… Commented Jun 19, 2015 at 18:05

1 Answer 1

3

The problem resides in that, whenever you have links to some entities, you must implement a repository for that entity too, in order to generate the proper links.

Sign up to request clarification or add additional context in comments.

2 Comments

I had the same infinite loop in my stacktrace, and when I created each repository for each entities that I had, it worked.
I spent two hours looking for mistakes. Your answer saved my afternoon. You MUST implement the repository for any referenced entity. Thanks...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.