如何在 Spring Boot Application 中使用 Rest Template 来管理多个端点?
How to use Rest Template in Spring Boot Application to manage multiple endpoints?
在我的 Spring 启动应用程序中,我只想用一种方法管理多个端点。
这些端点在我的 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"
在我的服务class中,我创建了两种不同的方法和 rest 模板,以这种方式单独管理每个端点
@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 级联。
你能帮助我吗??
我为我糟糕的英语道歉,我希望我已经解释了自己
你可以用这样的单一方法来做到这一点:
@RequestMapping(value = {"/template1", "/template2"}, method = RequestMethod.GET)
public void getWithRestTemplateGet1() {
//TODO
}
一个方法可以有多个请求映射。只需添加带有值列表的@RequestMapping 注释。
在我的 Spring 启动应用程序中,我只想用一种方法管理多个端点。 这些端点在我的 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"
在我的服务class中,我创建了两种不同的方法和 rest 模板,以这种方式单独管理每个端点
@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 级联。 你能帮助我吗?? 我为我糟糕的英语道歉,我希望我已经解释了自己
你可以用这样的单一方法来做到这一点:
@RequestMapping(value = {"/template1", "/template2"}, method = RequestMethod.GET)
public void getWithRestTemplateGet1() {
//TODO
}
一个方法可以有多个请求映射。只需添加带有值列表的@RequestMapping 注释。