title: Spring @Autowired restTemplate is null
body: <p>I am new to Spring. I develop Service that Consuming RESTful service with certficate using Java</p>
  
  <p>Here is my Config class:</p>
  
  <pre><code>package configuration;
  
  import org.apache.http.client.HttpClient;
  import org.apache.http.impl.client.HttpClients;
  import org.apache.http.ssl.SSLContextBuilder;
  import org.springframework.boot.web.client.RestTemplateBuilder;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.http.client.ClientHttpRequestFactory;
  import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
  import org.springframework.util.ResourceUtils;
  import org.springframework.web.client.RestTemplate;
  
  import javax.net.ssl.SSLContext;
  import java.util.function.Supplier;
  
  @Configuration
  public class RestClientCertConfig {
  
      private char[] allPassword = "allpassword".toCharArray();
  
      @Bean
      public RestTemplate restTemplate(RestTemplateBuilder builder) throws Exception {
  
          SSLContext sslContext = SSLContextBuilder
                  .create()
                  .loadKeyMaterial(ResourceUtils.getFile("classpath:keystore.jks"), allPassword, allPassword)
                  .loadTrustMaterial(ResourceUtils.getFile("classpath:truststore.jks"), allPassword)
                  .build();
  
          HttpClient client = HttpClients.custom()
                  .setSSLContext(sslContext)
                  .build();
  
          return builder
                  .requestFactory((Supplier&lt;ClientHttpRequestFactory&gt;)new HttpComponentsClientHttpRequestFactory(client))
                  .build();
      }
  }
  </code></pre>
  
  <p>And here is the class where I consume Restful EndPoint:</p>
  
  <pre><code>import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.http.*;
  import org.springframework.web.client.RestClientException;
  import org.springframework.web.client.RestTemplate;
  import java.net.URISyntaxException;
  import java.util.Collections;
  
  
  public class ECSConfigGet {
      private static final String ECS_API_URI = "&lt;RestEndPointToConsume";
  
      @Autowired
      private static RestTemplate restTemplate;
  
  
      public static void main(String[] args) {
          try {
              makeECSCall("myTestHeaderValue");
          } catch (URISyntaxException e) {
              e.printStackTrace();
          }
      }
  
  
      private static void makeECSCall(String entityCode) throws RestClientException, URISyntaxException {
          HttpHeaders headers = new HttpHeaders();
          headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
          headers.set("entityCode", entityCode);
  
          HttpEntity&lt;String&gt; entity = new HttpEntity&lt;&gt;("parameters", headers);
  
          ResponseEntity responseEntity  = restTemplate.exchange(ECS_API_URI, HttpMethod.GET, entity, String.class);
          }
      }
  </code></pre>
  
  <p>Did I completely misunderstood the concept? I would expect restTemplate would not be null with all the Annotations I use. Thank for any help!</p>
  
  <hr>
  
  <p>NullPointerException is fixed. ECSConfigGet looks like:</p>
  
  <pre><code>package main;
  
  import configuration.RestClientCertConfig;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.CommandLineRunner;
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  import org.springframework.http.*;
  import org.springframework.stereotype.Component;
  import org.springframework.web.client.RestClientException;
  import org.springframework.web.client.RestTemplate;
  import services.modelsdto.ExpenseConfigDTO;
  
  
  import java.util.Collections;
  
  @SpringBootApplication
  @Component
  public class ECSConfigGet implements CommandLineRunner{
  
      //API to call
      private static final String ECS_API_URI = "&lt;API_TO_CONSUME&gt;";
      @Autowired
      private RestTemplate restTemplate;
  
      public static void main(String[] args) {
  
          AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(RestClientCertConfig.class);
          applicationContext.getBean(RestTemplate.class);
          SpringApplication.run(ECSConfigGet.class, args);
  
      }
  
      private void makeECSCall(String entityCode) throws RestClientException {
  
          ExpenseConfigDTO expenseConfigDTO = new ExpenseConfigDTO();
  
          HttpHeaders headers = new HttpHeaders();
          headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
          headers.set("entityCode", entityCode);
  
          HttpEntity&lt;String&gt; entity = new HttpEntity&lt;&gt;("parameters", headers);
  
          ResponseEntity responseEntity  = restTemplate.exchange(ECS_API_URI, HttpMethod.GET, entity, String.class);
          }
  
      @Override
      public void run(String... args) {
          for (int i = 0; i &lt; args.length; ++i) {
              makeECSCall("myTestHeaderValue");
          }
      }
  }
  </code></pre>