0

The problem I want to solve. I need to apply a specific logic to all restful endpoints where the url belongs to a specific sub path: let's say "/api/employee/{id}". This means all the links which start with this path should apply a logic based on the employee ID, which I am trying to apply directly in Spring Boot filter in order to avoid to spread the logic everywhere.

The problem I face. I am able to get the query parameters from the ServletRequest, but the PathVariables are not available in the Filter.

Any idea how this could be parsed?

Would be much appreciated :)

X-HuMan
  • 1,488
  • 1
  • 17
  • 37

1 Answers1

1

The PathVariables are simply the URI. You cann call getRequestURI()

From the docs:

java.lang.String getRequestURI()

Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request. The web container does not decode this String. For example:

First line of HTTP request  Returned Value

POST /some/path.html HTTP/1.1       /some/path.html

GET http://foo.bar/a.html HTTP/1.0      /a.html

HEAD /xyz?a=b HTTP/1.1      /xyz

https://docs.oracle.com/javaee/7/api/javax/servlet/http/HttpServletRequest.html#getRequestURI--

Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82
  • I was eventually asking about a ready solution, but it helped to combine with REGEX and parse it. Thanks a lot. Another helping post from here. https://stackoverflow.com/questions/35500965/how-to-extract-path-parameter-value-in-uri – X-HuMan May 27 '20 at 10:01