Reputation: 1
2020-09-21 10:14:22,402 ip-172-31-2-31.us-west-2.compute.internal t=? http-nio-7067-exec-5 ERROR o.a.c.c.C.[.[.[.[dispatcherServlet].log - Servlet.service() for servlet [dispatcherServlet] in context with path [/sv] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException: null
at org.springframework.web.client.RestTemplate$AcceptHeaderRequestCallback.doWithRequest(RestTemplate.java:837)
at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:900)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:721)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:680)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:600)
at com.service.InstrumentService.getInstruments(InstrumentService.java:100)
at com.controller.InstrumentController.instrumentDetails(InstrumentController.java:49)
The issue occurs randomly after the application restart It will start working normally.
RestTemplate is Autowired in the class.
HttpEntity httpEntity = new HttpEntity(JsonUtil.toJson(payload), headers);
ResponseEntity<String> responseEntity = null;
try {
responseEntity = restTemplate.exchange(requestURI, HttpMethod.POST, httpEntity, String.class);
if (responseEntity.getStatusCode().is2xxSuccessful()) {
String responseBody = responseEntity.getBody();
List responseMap;
ObjectMapper mapper = new ObjectMapper();
responseMap = mapper.readValue(responseBody, new TypeReference<List>()
}
}
//main class
@Bean(name = "restTemplate")
public RestTemplate restTemplate() {
CloseableHttpClient httpClient = HttpClientBuilder.create ()
.setMaxConnTotal(
mfsConfiguration.getIntProperty("http.connection-pool.max-connection-total"))
.setMaxConnPerRoute(
mfsConfiguration.getIntProperty ("http.connection-pool.max-connection-per-route"))
.evictExpiredConnections()
.evictIdleConnections (
mfsConfiguration.getLongProperty("http.connection-pool.idle-timeout-in-seconds"),
TimeUnit.SECONDS)
.build ();
ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
RestTemplate restTemplate = new RestTemplate(requestFactory);
return restTemplate;
}
# http connection pool
http.connection-pool.max-connection-total=10
http.connection-pool.max-connection-per-route=5
http.connection-pool.idle-timeout-in-seconds=60
Upvotes: 0
Views: 2774
Reputation: 92
Actually, there should be a more detailed explanation but as far as I understand you should add RestTemplate as a Bean for example in your main class.
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
Upvotes: 2