LATEST EDIT
After many tests with many different services I suspect the hanging to be the cause of the headers. Perhaps the content length header. However I have no clue why is it behaving like this. I would need suggestions very much. I tried with services who return less headers and it does not hang.
I'm having a strange issue. I'm connecting to another service with a GET request using Resttemplate. However after sending the request, if I'm using a custom POJO as a ResponseEntity it takes about 30s to see the response. If I sent it using String as the ResponseEntity it responds quickly(up to 1s). I was wondering what could be the cause of this. The response format:
{
"data": [
{
"email": "eskaferas@gmail.com",
"firstName": "Seras",
"lastName": "Meras"
},
{
"email": "Soras@gmail.com",
"firstName": "Oras",
"lastName": "Moras"
},
{
"email": "bzbzb@gmail.com",
"firstName": "hello",
"lastName": "bye"
},
{
"email": "lrc@gmail.com",
"firstName": "Seras",
"lastName": "Meras"
}
],
"message": "Success"
}
My POJO:
public class ArrayResponsePojo {
private User[] data;
private String message;
public User[] getData() {
return data;
}
public void setData(User[] data) {
this.data = data;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public ArrayResponsePojo(){
}
}
User class:
public class User {
private String email;
private String firstName;
private String lastName;
public User(){
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
The get method:
@GetMapping("/todos/{toDoNoteId}/users")
public ResponseEntity getNotesUsersTest(@PathVariable int toDoNoteId) {
ToDoNote note = toDoNoteService.getToDoNoteById(toDoNoteId);
if(note==null) {
throw new ToDoNoteNotFoundException("Note with id "+ toDoNoteId + " not found");
}
RestTemplate restTemplate = new RestTemplate();
final String uri = "http://friend:5000/users";
try {
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<String>("parameters", requestHeaders);
ResponseEntity<ArrayResponsePojo> result = restTemplate.exchange(uri, HttpMethod.GET,entity, ArrayResponsePojo.class);
return result;
}
catch (HttpClientErrorException ex) {
return ResponseEntity.status(ex.getRawStatusCode()).headers(ex.getResponseHeaders())
.body(ex.getResponseBodyAsString());
}
}
Any ideas are welcome. I would really appreciate the help. Link to my previous issue perhaps it is linked? That issue is solved, if not for the slow speed. Error "Could not extract response: no suitable HttpMessageConverter found for response type ... and and content type [application/json]"
EDIT I noticed that it responds very quickly in the browser. But I'm using Postman and using Postman it is super slow. I'm really lost at this point since no errors are thrown or anything.
EDIT 2 The only thing I can see from the debugger is that it hangs for a long time on the method afterExecute(task, thrown); in class java.util.concurrent.ThreadPoolExecutor. This is really frustrating :(
EDIT 3 I think it has something to do with threads but running debug a 2nd time it hanged on a different method in java.util.concurrent.LockSupport class called
public static void park(Object blocker) {
Thread t = Thread.currentThread();
setBlocker(t, blocker);
UNSAFE.park(false, 0L);
setBlocker(t, null);
}
But I don't understand what could be wrong it would take about 30s... It is actually taking a whole minute. And why does the response work normally if you pass String instead of my custom POJO in response entity and restTemplate.
Another EDIT I tried to get the response with curl and it gets the headers gets the response and then just hangs for a minute. I'm not sure what could be happening at that point. After the minute it exits with message "curl: (18) transfer closed with 79 bytes remaining to read"
Already tried setting read and connect timeouts did not help, tried connecting to different services which respond with different type POJOs, it is still the same.