jeudi 3 octobre 2019

Spring: How to decorate some incoming requests with extra logic

I don't know how to ask this without getting downvoted 10 times, and being clear but here it goes. I have a rest controller class with many endpoints doing certain operations on books depending on action.

My question is a design question, how do I best get the all action specific business logic out from the abstract controller class(handleRequest method)? One of options I considered is GOF Template pattern which can fit here nicely. Problem with that is I dont want to create a different implementation class for each action type. Maybe %80 of the incoming action types have a generic flow. I only want to create an exception for certain action types and add some extra logic to them. Another option is Intercepting Filter pattern, still don't fit well to my situation.

mainController extends AbstractController{
   ....

  @PostMapping(value = "/getBooks/")
   public getBooks( @PathVariable ActionType action){
       handleGenericBookRequests(action)
  }

  ....

}

abstract class AbstractController{

 handleRequest(action){
  //do mostly generic stuff
  //do generic stuff
 if (action1){
  //do some action1 specific stuff  using action 1 related services
 }
 if(action2){
  //do some action2 specific stuff  using action 2 related services
 }

 //continue
 ..
 }
}

Aucun commentaire:

Enregistrer un commentaire