春天@Autowired restTemplate为空[复制](Spring @Autowired

2019-11-05 02:35发布

这个问题已经在这里有一个答案:

  • 为什么我的春天@Autowired场空? 15个回答

我是新来的春天。 我开发服务,食用使用Java certficate RESTful服务

这里是我的配置类:

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<ClientHttpRequestFactory>)new HttpComponentsClientHttpRequestFactory(client))
                .build();
    }
}

这里是我消耗宁静的端点类:

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 = "<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<String> entity = new HttpEntity<>("parameters", headers);

        ResponseEntity responseEntity  = restTemplate.exchange(ECS_API_URI, HttpMethod.GET, entity, String.class);
        }
    }

难道我完全误解的概念? 我希望restTemplate就不会是空了所有我用的注解。 感谢您的帮助!


NullPointerException异常是固定的。 ECSConfigGet的样子:

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 = "<API_TO_CONSUME>";
    @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<String> entity = new HttpEntity<>("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 < args.length; ++i) {
            makeECSCall("myTestHeaderValue");
        }
    }
}

Answer 1:

你错过了一下春天的样板,你需要让@Autowired工作。 如果你使用Spring启动,你接近,但@Patrick是对一般: ECSConfigGet需要通过正确标注它是一个bean,但你也需要为了运行一个应用程序环境中的应用程序对于任何春神奇的事情发生。 我建议您查看本教程如何在一个命令行应用程序使用Spring启动。

高级别ECSConfigGet需要被注释@SpringBootApplication ,然后让它执行CommandLineRunner然后从run的方法,你将有机会获得@Autowired组件。 Spring将实例 ECSConfigGet和填充性。 另外,作为@Roddy指出, RestTemplate不能是静态的,无论是。



Answer 2:

该ECSConfigGet类不是一个bean,因此它不能自动装配的组件。 添加@Component作为类注释ECSConfigGet



文章来源: Spring @Autowired restTemplate is null [duplicate]