Reputation: 197
In my Spring Boot Application I want to manage in only one method multiple endpoints. These endpoints are declared in my application.yml in this way:
spring:
username: xxx
password: acb132
route:
source:
protocol: https://
ip: 10.xxx.y.zz/
root: "swdfr/"
paths: >
- "ofh/ert/hAFG5"
- "ofh/ert/ryt54"
In my Service class I have created two different method with rest template to manage every endpoint individually, in this way
@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());
}
}
But I want to call the two endpoints in a single method with maybe a switch or a cascade of if. Can you help me?? I apologize for my bad English and I hope I have explained myself
Upvotes: 1
Views: 511
Reputation: 1015
You can do this with single method like this:
@RequestMapping(value = {"/template1", "/template2"}, method = RequestMethod.GET)
public void getWithRestTemplateGet1() {
//TODO
}
You can have multiple request mappings for a method. Just add @RequestMapping annotation with a list of values.
Upvotes: 3