0

I have a controller that exposes the following endpoint:

@RequestMapping("/all-users")
List<User> getAllUsers() {
    ...
}

I have also an annotation that helps me out with versioning of those endpoints, which ends up on something like this:

@RequestMapping("/all-users")
@Version(version=1, latests=LATEST_ALL_USERS)
List<User> getAllUsers() {
    ...
}

Now I want to introduce an additional standard behavior to all handlers mapped wish method contains @Version annotation which will simply wrap the response object into another object which contains the current version and latest version of the invoked method. Some information to build this object are provided by @PathVariable parameters. I'm trying to find a hook that allows me that but no luck so far.

I tried first to have a custom RequestResponseBodyMethodProcessor but if I add it will not take any effect because the original RequestResponseBodyMethodProcessor comes before and I don't want to remove the ResponseBody from my endpoints.

Afterward I tried to go for the mapping instead, once I cannot handle it on the processor, maybe I could handle that on mapping time introducing my code pre and post method invocation, but got stuck on the point where mapping is registered where a method object is needed, not allowing me to introduce my advice code.

Is there any way to get this done?

Edit:

Some of the information needed to build the new returned object are provided as @PathVariables, and are available on end-point method call.

Francisco Spaeth
  • 23,493
  • 7
  • 67
  • 106
  • What about [`ResponseBodyAdvice`](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/mvc/method/annotation/ResponseBodyAdvice.html)? Btw, why not to use some header instead? – Dmitry Senkovich Sep 08 '17 at 13:24
  • nice approach, but I totally forgot to comment that some information are gathered from the controller endpoint invocation (like `@PathVariable`) – Francisco Spaeth Sep 08 '17 at 15:25
  • Well, I still don't see a problem:) – Dmitry Senkovich Sep 08 '17 at 16:12
  • @DmitrySenkovich: ok, sorry, maybe I have overseen something, but figure out how to access a `@PathVariable` for the invocation using its name from an implementation of `ResponseBodyAdvice`. Can you point out a solution for that? – Francisco Spaeth Sep 11 '17 at 07:33
  • well, my guess is simple: you can't. But you can use 'request' – Dmitry Senkovich Sep 11 '17 at 08:09
  • I see. But I really need that information using annotated parameter. – Francisco Spaeth Sep 11 '17 at 08:26
  • I would end up with headers then. Why do you have to store such information in response body? As for me it is not that kind of information that should be returned in body. it is some kind of metadata. I've never seen storing version related information in body – Dmitry Senkovich Sep 11 '17 at 09:59
  • but actually you can try to use the following code: `Map pathVariables = (Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);` – Dmitry Senkovich Sep 11 '17 at 10:01

0 Answers0