Let's say I have a controller which handles requests such as www.xyz.com/api/<someParam>
. This is my controller, and my Service:
@Controller
public class MyController {
@Autowired MyService service;
@RequestMapping(value = "/api/{someParam}", method = RequestMethod.GET)
public String processRequest(
@PathVariable("someParam") String someParam) {
return service.processRequest(someParam);
}
}
@Service
public class MyService {
@Autowired APICaller apiCaller;
public String processRequest(someParam){
SomeObj obj = apiCaller.callApi();
// do something with obj
// return response;
}
}
Based on the param passed in the URL, I need to call some API, do some processing to the API response, and return it. All these APIs have different processing.
Let's say the APICaller interface is like this:
@Service
public interface APICaller {
public SomeObj callAPI();
}
@Service
public class ABC implements APICaller {
@Override
public SomeObj callAPI() {
// calls some REST api, does some processing to response and returns SomeObj
}
}
@Service
public class XYZ implements APICaller {
@Override
public SomeObj callAPI() {
// calls some SOAP api, does some processing to response and returns SomeObj
}
}
So if the param in the url is 'abc', I need to call ABCImpl
. And if it is 'xyz', then I need to call XYZImpl
. What should I do in the MyService
class to instantiate the proper implementation? I might have multiple implementations based on the param, not just these two.
Thanks.