5

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.

  • check if you have duplicate libs – Ajay Kumar Apr 19 '19 at 19:09
  • @Ajay Kumar in what part/place of my Spring boot project? – Svajunas Kavaliauskas Apr 19 '19 at 19:10
  • @SvajunasKavaliauskas You're saying `ResponseEntity` is faster than `ResponseEntity`? – Pasupathi Rajamanickam Apr 19 '19 at 19:18
  • @Pasupathi Rajamanickam ResponseEntity under 1s. ResponseEntity 60s. Also i tried sending the request with curl, it prints the response then hangs at the end of line for a minute and then prints "transfer closed with 79 bytes remaining to read" – Svajunas Kavaliauskas Apr 19 '19 at 19:22
  • in pom/yml of your project – Ajay Kumar Apr 19 '19 at 19:26
  • @Ajay Kumar I only have several warnings in the pom for example duplicate manage version 2.8.5 for gson but I cant remove it since then I can't import Gson at all. – Svajunas Kavaliauskas Apr 19 '19 at 19:46
  • try to fix the warnings. that could be a possible reason for it. – Ajay Kumar Apr 19 '19 at 20:22
  • @Ajay Kumar Did not help sadly... I added an EDIT maybe have any idea what could be the cause of it? – Svajunas Kavaliauskas Apr 19 '19 at 20:31
  • @SvajunasKavaliauskas sounds like your server is not closing connection on time looking at your curl response. By default RestTemplate does not have read time out, if you add read timeout 2s may help you to down 30 to 2. Since it's happening for curl as well, I would rather suspect API than client. – Pasupathi Rajamanickam Apr 19 '19 at 20:46
  • @Pasupathi Rajamanickam I tried with other services and it is the same so it must be in my application :/. The services I'm connecting to are written in python flask. – Svajunas Kavaliauskas Apr 19 '19 at 20:49
  • @Pasupathi Rajamanickam Setting a readTimeout and ConnectTimeout did not help as well. – Svajunas Kavaliauskas Apr 19 '19 at 20:57
  • 1
    @SvajunasKavaliauskas Ya, the cross tech `RestTemplate` always having issues, you may see lot of fun if you try to connect ASP API. :D I would suggest you to go for `ResponseEntity` API calls are faster and use ObjectMapper to create object. – Pasupathi Rajamanickam Apr 19 '19 at 21:08
  • @SvajunasKavaliauskas one last try. Try `List` instead array and see if any improvement. – Pasupathi Rajamanickam Apr 19 '19 at 21:10
  • @Pasupathi Rajamanickam So the cause could be since I'm connecting to a API written in another language? I considered using RestTemplate, but I'm worried about my POST requests. I will need to post those user objects to the other web service. – Svajunas Kavaliauskas Apr 19 '19 at 21:12
  • @Pasupathi Rajamanickam Sorry, already tried it some time ago, forgot to mention... So there is no saving it would seem. Perhaps you know of any alternatives? Should I just get String responses and convert them to my POJOs? What about POST requests when I would need to post the POJO? – Svajunas Kavaliauskas Apr 19 '19 at 21:14
  • 1
    @SvajunasKavaliauskas I faced issue when connecting to ASP.NET api, I guessing you're running same. Here is the fun https://stackoverflow.com/questions/50996742/java-char-encoding-for-strange-string-from-api For POST also you can use `ObjectWriter` to convert object to json string. – Pasupathi Rajamanickam Apr 19 '19 at 21:17
  • Sorry when you say tried `List` ? – Pasupathi Rajamanickam Apr 19 '19 at 21:19
  • @Pasupathi Rajamanickam I did not mention it in the post, but I tried and it did not help. – Svajunas Kavaliauskas Apr 19 '19 at 21:20
  • @Pasupathi Rajamanickam It would seem that it is actually to do with something between cross platform, because I tried to connect to another spring web service and it has worked. But it also might be with the headers since this one does not return content length for example. I guess I have no other choice as to use RestTemplate with String. Atleast for now, maybe someone will suggest something later. Thanks for your suggestions :) – Svajunas Kavaliauskas Apr 19 '19 at 21:35
  • Your last two edits appear to be changes in order to attract attention, rather than to improve it. Periodic cosmetic edits are not constructive and needlessly bump your post, displacing actually active posts that require more community attention. Please only edit your post to correct errors, to include additional insights or to update the question for changing circumstances. If you continue to only edit it for cosmetic reasons only, we'll have to lock your post from all edits. – Bhargav Rao Apr 20 '19 at 15:43
  • @Bhargav Rao Alright, understood. – Svajunas Kavaliauskas Apr 20 '19 at 16:59

0 Answers0