在我的 Spring Boot 应用程序中,我只想用一种方法管理多个端点。这些端点以这种方式在我的 application.yml 中声明:
spring:
username: xxx
password: acb132
route:
source:
protocol: https://
ip: 10.xxx.y.zz/
root: "swdfr/"
paths: >
- "ofh/ert/hAFG5"
- "ofh/ert/ryt54"
在我的服务类中,我创建了两种不同的方法,使用休息模板来单独管理每个端点,以这种方式
@ResponseBody
public void getWithRestTemplateGet1() {
final String methodName = "getWithRestTemplateGet1()";
try {
startLog(methodName);
String url = protocol + ip + root + paths.get(0);
HttpHeaders headers = new HttpHeaders();
headers.setBasicAuth(username, password);
HttpEntity request = new HttpEntity(headers);
try {
RestTemplate restTemplate;
if (url.startsWith("https")) {
restTemplate = getRestTemplateForSelfSsl();
} else {
restTemplate = new RestTemplate();
}
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, request, String.class);
HttpStatus statusCode = response.getStatusCode();
logger.info("STATUS: " + statusCode);
} catch (HttpStatusCodeException e) {
logger.error(e.getMessage());
}
endLog(methodName);
} catch (Exception e) {
logger.error(e.getMessage());
}
}
@ResponseBody
public void getWithRestTemplateGet2() {
final String methodName = "getWithRestTemplateGet2()";
try {
startLog(methodName);
String url = protocol + ip + root + paths.get(1);
HttpHeaders headers = new HttpHeaders();
headers.setBasicAuth(username, password);
HttpEntity request = new HttpEntity(headers);
try {
RestTemplate restTemplate;
if (url.startsWith("https")) {
restTemplate = getRestTemplateForSelfSsl();
} else {
restTemplate = new RestTemplate();
}
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, request, String.class);
HttpStatus statusCode = response.getStatusCode();
logger.info("STATUS: " + statusCode);
} catch (HttpStatusCodeException e) {
logger.error(e.getMessage());
}
endLog(methodName);
} catch (Exception e) {
logger.error(e.getMessage());
}
}
但我想在一个方法中调用两个端点,可能是一个开关或一个 if 级联。你能帮助我吗??我为我糟糕的英语道歉,我希望我已经解释了自己