gpt4 book ai didi

java - Spring @Autowired RestTemplate 为 null

转载 作者:行者123 更新时间:2023-12-01 17:49:43 24 4
gpt4 key购买 nike

我是 Spring 新手。我使用 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;

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

这是我使用 Restful EndPoint 的类:

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");
}
}
}

最佳答案

您缺少一些使 @Autowired 工作所需的 Spring 样板文件。如果您使用 Spring Boot,那么您已经很接近了,但 @Patrick 通常是正确的:ECSConfigGet 需要通过正确注释来成为一个 bean,但您还需要在应用程序上下文中运行应用程序为了让任何 Spring 的魔法发生。我建议查看this tutorial了解如何在命令行应用程序中使用 Spring Boot。

高层是ECSConfigGet需要用@SpringBootApplication注释,然后让它实现CommandLineRunner,然后从运行 方法,您将可以访问 @Autowired 组件。 Spring 将实例化 ECSConfigGet 并填充属性。另外,正如 @Roddy 指出的,RestTemplate 也不能是静态的。

关于java - Spring @Autowired RestTemplate 为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51756411/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com