繁体   English   中英

Spring启动应用程序中的RestTemplate空指针异常

[英]RestTemplate Null pointer exception In Spring boot Application

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)

应用程序重新启动后随机出现该问题它将开始正常工作。

RestTemplate 在类中是自动装配的。

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>()
           }
}

//主类

@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

实际上,应该有更详细的解释,但据我所知,您应该在主类中添加 RestTemplate 作为 Bean 例如。

@SpringBootApplication
public class DemoApplication {
   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }
   @Bean
   public RestTemplate getRestTemplate() {
      return new RestTemplate();
   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM