Spring 引导:使用 Rest 模板管理多个端点?
Spring Boot: Manage Multiple Endpoint with Rest Template?
在我的 Spring 引导项目中,我在应用程序-dev.yml 文件中设置了几个端点(引用一些 GET 或 POST REST API)。
spring:
username: xxx
password: acb132
route:
source:
protocol: https://
ip: 10.xxx.y.zz/
root: "swdfr/"
paths: >
- "ofh/ert/hAFG5"
- "ofh/ert/ryt54"
我想使用一种方法在服务 class 中管理这些端点。目前我已经实现了这个解决方案:
//REST CONTROLLER
@GetMapping("/Multiple_Get")
public void manageGetEndpointsWithRestTemplate() throws Exception{
final String methodName = "manageGetEndpointsWithRestTemplate()";
try {
service.manageGetEndpointsWithRestTemplate();
} catch (final Exception e) {
this.errorLog(methodName, e);
throw e;
}
}
//SERVICE
@ResponseBody
public void manageGetEndpointsWithRestTemplate() {
final String methodName = "manageGetEndpointsWithRestTemplate()";
try {
String urlGet1 = protocol + ip + root + paths.get(0);
String urlGet2 = protocol + ip + root + paths.get(1);
HttpHeaders headers = new HttpHeaders();
headers.setBasicAuth(username, password);
HttpEntity request = new HttpEntity(headers);
try {
RestTemplate restTemplate;
if (urlGet1.startsWith("https") || urlGet2.startsWith("https")) {
restTemplate = getRestTemplateForSelfSsl();
} else {
restTemplate = new RestTemplate();
}
// GET1
ResponseEntity<String> response1 = restTemplate.exchange(urlGet1, HttpMethod.GET, request,
String.class);
HttpStatus statusCode1 = response1.getStatusCode();
logger.info("STATUS GET1: " + statusCode1);
// GET2
ResponseEntity<String> response2 = restTemplate.exchange(urlGet2, HttpMethod.GET, request,
String.class);
HttpStatus statusCode2 = response2.getStatusCode();
logger.info("STATUS GET2: " + statusCode2);
} catch (HttpStatusCodeException e) {
logger.error(e.getMessage());
}
} catch (Exception e) {
logger.error(e.getMessage());
}
}
public RestTemplate getRestTemplateForSelfSsl()
throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
TrustStrategy acceptingTrustStrategy = (X509Certificate[] x509Certificates, String s) -> true;
SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy)
.build();
SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext, new NoopHostnameVerifier());
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(csf).build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
RestTemplate restTemplate = new RestTemplate(requestFactory);
return restTemplate;
}
我想使用一种方法使其尽可能具有通用性,尤其是当我必须管理大量端点时。
你有什么想法?
提前致谢
您可以遍历所有 paths
并执行通用代码,考虑到您正在对所有请求执行 GET
。
@ResponseBody
public void manageGetEndpointsWithRestTemplate() {
final String methodName = "manageGetEndpointsWithRestTemplate()";
try {
paths.forEach(path -> {
String urlGet = protocol + ip + root + path;
HttpHeaders headers = new HttpHeaders();
headers.setBasicAuth(username, password);
HttpEntity request = new HttpEntity(headers);
try {
RestTemplate restTemplate = urlGet.startsWith("https") ? getRestTemplateForSelfSsl() : new RestTemplate();
ResponseEntity<String> response = restTemplate.exchange(urlGet, HttpMethod.GET, request,
String.class);
HttpStatus statusCode = response.getStatusCode();
logger.info("STATUS GET - {} : {}", urlGet, statusCode);
} catch (HttpStatusCodeException e) {
logger.error(e.getMessage());
}
});
} catch (Exception e) {
logger.error(e.getMessage());
}
}
如果您要使用 POST
或任何其他 HTTP 方法,请在 application-dev.yml
文件中与 paths
一起提及。在传递到 restTemplate.exchange()
.
之前添加一些额外的逻辑来确定 HTTP.XXX
在我的 Spring 引导项目中,我在应用程序-dev.yml 文件中设置了几个端点(引用一些 GET 或 POST REST API)。
spring:
username: xxx
password: acb132
route:
source:
protocol: https://
ip: 10.xxx.y.zz/
root: "swdfr/"
paths: >
- "ofh/ert/hAFG5"
- "ofh/ert/ryt54"
我想使用一种方法在服务 class 中管理这些端点。目前我已经实现了这个解决方案:
//REST CONTROLLER
@GetMapping("/Multiple_Get")
public void manageGetEndpointsWithRestTemplate() throws Exception{
final String methodName = "manageGetEndpointsWithRestTemplate()";
try {
service.manageGetEndpointsWithRestTemplate();
} catch (final Exception e) {
this.errorLog(methodName, e);
throw e;
}
}
//SERVICE
@ResponseBody
public void manageGetEndpointsWithRestTemplate() {
final String methodName = "manageGetEndpointsWithRestTemplate()";
try {
String urlGet1 = protocol + ip + root + paths.get(0);
String urlGet2 = protocol + ip + root + paths.get(1);
HttpHeaders headers = new HttpHeaders();
headers.setBasicAuth(username, password);
HttpEntity request = new HttpEntity(headers);
try {
RestTemplate restTemplate;
if (urlGet1.startsWith("https") || urlGet2.startsWith("https")) {
restTemplate = getRestTemplateForSelfSsl();
} else {
restTemplate = new RestTemplate();
}
// GET1
ResponseEntity<String> response1 = restTemplate.exchange(urlGet1, HttpMethod.GET, request,
String.class);
HttpStatus statusCode1 = response1.getStatusCode();
logger.info("STATUS GET1: " + statusCode1);
// GET2
ResponseEntity<String> response2 = restTemplate.exchange(urlGet2, HttpMethod.GET, request,
String.class);
HttpStatus statusCode2 = response2.getStatusCode();
logger.info("STATUS GET2: " + statusCode2);
} catch (HttpStatusCodeException e) {
logger.error(e.getMessage());
}
} catch (Exception e) {
logger.error(e.getMessage());
}
}
public RestTemplate getRestTemplateForSelfSsl()
throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
TrustStrategy acceptingTrustStrategy = (X509Certificate[] x509Certificates, String s) -> true;
SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy)
.build();
SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext, new NoopHostnameVerifier());
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(csf).build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
RestTemplate restTemplate = new RestTemplate(requestFactory);
return restTemplate;
}
我想使用一种方法使其尽可能具有通用性,尤其是当我必须管理大量端点时。 你有什么想法? 提前致谢
您可以遍历所有 paths
并执行通用代码,考虑到您正在对所有请求执行 GET
。
@ResponseBody
public void manageGetEndpointsWithRestTemplate() {
final String methodName = "manageGetEndpointsWithRestTemplate()";
try {
paths.forEach(path -> {
String urlGet = protocol + ip + root + path;
HttpHeaders headers = new HttpHeaders();
headers.setBasicAuth(username, password);
HttpEntity request = new HttpEntity(headers);
try {
RestTemplate restTemplate = urlGet.startsWith("https") ? getRestTemplateForSelfSsl() : new RestTemplate();
ResponseEntity<String> response = restTemplate.exchange(urlGet, HttpMethod.GET, request,
String.class);
HttpStatus statusCode = response.getStatusCode();
logger.info("STATUS GET - {} : {}", urlGet, statusCode);
} catch (HttpStatusCodeException e) {
logger.error(e.getMessage());
}
});
} catch (Exception e) {
logger.error(e.getMessage());
}
}
如果您要使用 POST
或任何其他 HTTP 方法,请在 application-dev.yml
文件中与 paths
一起提及。在传递到 restTemplate.exchange()
.
HTTP.XXX