2

Trying to send response status codes metrics per API using spring HandlerInterceptorAdapter postHandle:

@Override
public void postHandle(
    HttpServletRequest request,
    HttpServletResponse response,
    Object handler,
    ModelAndView modelAndView
)

Works very well with success responses, however exceptions are all handled by a different controller so from what i can see - spring seems to redirect to a different servlet with the route /error so the original request is lost.

Is there a way to get the original request object at that point from within the postHandle method?

The only direction i have at this point is getting the original request from the response but could not figure out how to do it as HttpServletResponse does not expose getRequest.

EDIT: I need the original request object and not just the url so that i'll be able to get the path variables and remove them from the metric name.

Daniel
  • 6,194
  • 7
  • 33
  • 59
  • @Andreas - edited, i need the original request **object** for the path variables map. – Daniel Jun 13 '19 at 07:47
  • Why do you need the original request object? You asked for the original URL, and the link gives you that. Now you say you need the URL to get the path variables, but you've already been told how to get the URL, so I don't see why you need the original request object? – Andreas Jun 13 '19 at 07:55
  • Yeah sorry about that, updated the question. I need to remove the path variables from the url to have a unified metric name, in order to do that i need to know what the path variables are, if i had the request object i could use `request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE)` – Daniel Jun 13 '19 at 07:58
  • @Andreas, could you please unmark it (or let me know if you think it's still a duplicate)? – Daniel Jun 13 '19 at 09:58
  • Removed duplicate: [Get URL of page requested that caused a 404](https://stackoverflow.com/q/4948275/5221149) – Andreas Jun 13 '19 at 14:10

0 Answers0