2

I am migrating to a RestController from an old lib implemented over Netty.

I used to have an ability in the controller to get the entire Request object and pass it down. Than later logic layers take whatever they need from the headers and query params.

dosomething(request: Request, someQueryParam: String) {
if(someQueryParam!=null) { doAnotherThing(request) }
}
doAnotherThing(request: Request) { .. use more query params and headers ..}

Imagine this has numerous params and pass through more classes. How will I be able to do it now without exposing the whole list of params used in the bottom, on every endpoint signature? (and without having to move my api users to use requestBody)

tw_ob
  • 61
  • 6

1 Answers1

2

Spring offers you a lot of options.

First of all: You don't have to carry around your request. In every place (except of multithreading) you can get the current request.
Is there a static way to get the current HttpServletRequest in Spring

Otherwise you can also add HttpServletRequest request as parameter to your controller-method to obtain the request and pass it as parameter to the service.
Spring 3 MVC accessing HttpRequest from controller

But a lot nicer approach is to fetch the required fields on controller-level and pass it as parameters to services. Spring can help you with a lot of annotations.

  1. @RequestHeader
  2. @RequestParam
  3. @PathVariable
  4. Request mapping in general

Then you can write nice JUnit-Tests and you have nice interfaces and services are not bound to HttpRequests. There is not only REST in the world.

akop
  • 5,981
  • 6
  • 24
  • 51