Below is the logic that I used for RestTemplate. For 1000 requests made, 50 requests fail intermittently. I have to trigger a GET request which sends List<Students> in the response. Hence used ParameterizedTypeReference.
The logger "Outbound request" displays in the logs and after that I received:org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.StackOverflowError.
The logger "Inbound response" is not displaying.
Code:
private RestTemplate restTemplate;
public List<Students> getConsents(Long managementid) {
List<Students> studentList = new ArrayList<>();
RequestEntity<String> requestEntity = buildRequestEntity(managementid);
ResponseEntity<List<Students>> response = null;
LOGGER.info("Outbound request");
response = restTemplate.exchange(requestEntity, new ParameterizedTypeReference<List<Students>>() {});
LOGGER.info("Inbound response");
if(null != response && response.getStatusCode().is2xxSuccessful())
studentList = response.getBody();
return studentList;
}
private RequestEntity<String> buildRequestEntity(Long managementid) {
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
URI uri = UriComponentsBuilder.fromHttpUrl(url)
.queryParam("managementid", managementid)
.build()
.toUri();
return new RequestEntity<>(headers, HttpMethod.GET, uri);
}
Stack Trace:
org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.StackOverflowError
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1053)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at ....
.....
at java.base/sun.nio.ch.Invoker$2.run(Invoker.java:219)
at java.base/sun.nio.ch.AsynchronousChannelGroupImpl$1.run(AsynchronousChannelGroupImpl.java:112)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.StackOverflowError: null
at java.base/java.util.Collections$UnmodifiableCollection.isEmpty(Collections.java:1033)
at java.base/java.util.Collections$UnmodifiableCollection.isEmpty(Collections.java:1033)
at java.base/java.util.Collections$UnmodifiableCollection.isEmpty(Collections.java:1033)
at java.base/java.util.Collections$UnmodifiableCollection.isEmpty(Collections.java:1033)
at java.base/java.util.Collections$UnmodifiableCollection.isEmpty(Collections.java:1033)
at java.base/java.util.Collections$UnmodifiableCollection.isEmpty(Collections.java:1033)
at java.base/java.util.Collections$UnmodifiableCollection.isEmpty(Collections.java:1033)
at java.base/java.util.Collections$UnmodifiableCollection.isEmpty(Collections.java:1033)
......
Please help me if I have used the RestTemplate wrong or if you know why I am receiving this error. This occurs in our production environment and I'm not able to reproduce it in our test environment. All the exception handling is done, I just did not paste it here.