1

I'm trying to get some data from a simple ajax object. I return objects like this many times but this one is giving me

GET http://localhost:8080/loggedUsers net::ERR_INCOMPLETE_CHUNKED_ENCODING

With the error on the server...

java.lang.StackOverflowError
at java.lang.Exception.<init>(Exception.java:66)
at java.io.IOException.<init>(IOException.java:58)
at com.fasterxml.jackson.core.JsonProcessingException.<init>(JsonProcessingException.java:25)
at com.fasterxml.jackson.core.JsonProcessingException.<init>(JsonProcessingException.java:41)
at com.fasterxml.jackson.databind.JsonMappingException.<init>(JsonMappingException.java:251)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:734)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)

....tons of times

If I put a breakpoint on the return it seems everything I want is there. And there's only 1 user so it's not much data.

Controller

  @RequestMapping(value = "/loggedUsers", method = RequestMethod.GET)
public @ResponseBody List<User> getLoggedUsers() {
    Map<String,User> users = User.getUsers().stream().collect(Collectors.toMap(User::getUsername, Function.identity()));
    List<User> onlineUsers = new ArrayList<>();
    for(String username : activeUsers.getUsers()) {
        User user = users.get(username);
        if(user != null) {
            onlineUsers.add(user);
        }
    }
    return onlineUsers;
}

AJAX

 //get loggedUsers
$.ajax({
    url: '/loggedUsers',
    type: 'get',
    success: function (loggedUsers) {
        console.log(loggedUsers);    
    }
});
  • The user object has the decodedImage that is a big String64 string but I don't think that would be a problem... –  Apr 17 '18 at 15:19
  • **i see the error** my User Object has userRole and userRole has object of User. so it's an infinite loop. What now?? –  Apr 17 '18 at 15:20
  • Could you attach also the class User? I guess could be an internal reference to an User as a class field – cdr89 Apr 17 '18 at 15:23
  • 2
    Possible duplicate of [How to solve this infinite recursion (StackoverflowError) when I use writeValueAsString() method of Jackson ObjectMapper() class?](https://stackoverflow.com/questions/32647849/how-to-solve-this-infinite-recursion-stackoverflowerror-when-i-use-writevaluea) – Arnaud Apr 17 '18 at 15:24

1 Answers1

0

If you have an internal reference to an User in the user class you could ignore that properties (using the @JsonIgnore annotation for example) and prevent the stack overflow, for more details: http://www.baeldung.com/jackson-ignore-properties-on-serialization

cdr89
  • 958
  • 9
  • 18