P
P
parkito2017-01-31 22:56:49
Java
parkito, 2017-01-31 22:56:49

How to avoid infinite recursion when rendering json in spring?

Hello. Help, please, to solve a problem. There are two entities, each containing a list of the other.

public class UserDTO implements Serializable {
    private String name;
    private String secondName;
    private String email;

    private List<Group> groups;

}

public class GroupDTO {
    private String title;
    private List<User> users;

The problem is that when I translate one entity containing a list to another, then I get an infinite recursion
@RequestMapping(value = "/getAllUsers", method = RequestMethod.GET)
    @ResponseBody
    public UserDto getAllUsers(HttpServletRequest req) {
        return userService.getUser("1");
    }

How can you avoid it. For example, Hibernate in such a situation understands what is required of it.
Tried annotations like
@JsonBackReference
@JsonManagedReference

They remove recursion, but at the same time lists of dependent entities are no longer displayed (user groups are not displayed)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2017-02-04
@dsv

And here they write what it seems should work with the specified annotations: keenformatics.blogspot.ru/2013/08/how-to-solve-jso...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question