I designed a framework to expose some rest based webservices. The base classes looked like this (not exact code)
public interface WSService {
public void init(WSRequest request);
public List<String> validate();
public void performAction();
public WSResponse generateResponse();
}
Each new service, implements this webservice and implements its specific logic
public class WSOrders implements WSService {...}
Then, i implemented a WSProcessor
which takes in specific class and then execute the methods and performs error checks/etc. for example
@RestResource(GET, "/getorders")
public Response getOrders (...){
WSService getOrders = new WSOrders();
WSRequestProcessor processor = new WSRequestProcessor(restInput);
processor.process(getOrders);
}
in WSRequestProcessor
, i used the process method to first extract input from rest request, do application level validation (some headers validation etc). Then called metthods like this
public WSRequestProcessor{
...
public void process (WSService service){
service.init()
service.validate();
service.performAction();
service.generateResponse();
}
}
One thing to note that WSRequestProcessor
handles all the exception handling, generic error response creation etc.
This pattern worked out pretty well to expose any of the resource for us. At the end of the day, to expose any resource, all i had to (or any developer) was to create WS<ResourceName>
and implement their business logic there.
Aucun commentaire:
Enregistrer un commentaire