为了账号安全,请及时绑定邮箱和手机立即绑定

Spring @Autowired restTemplate 为空

Spring @Autowired restTemplate 为空

ibeautiful 2021-06-28 17:35:07
我是春天的新手。我开发了使用 Java 使用证书使用 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;@Configurationpublic 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();    }}
查看完整描述

2 回答

  • 2 回答
  • 0 关注
  • 606 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信