5

I am trying to implement ContainerRequestFilter and the method filter.

How can I extract he path params from ContainerRequest request?

I can only see a direct method to extract the query and form parameters.

Dejell
  • 13,947
  • 40
  • 146
  • 229

2 Answers2

4

ContainerRequest doesn't seem to contain that information, but you can inject a UriInfo into the filter and use that. See UriInfo.getPathParameters().

Reference: How to get the value of a PathParam outside a Resource class

Sridhar Sarnobat
  • 25,183
  • 12
  • 93
  • 106
Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
  • Regarding the link to an answer from the forums - the links do not work. – Dejell Jun 03 '13 at 21:57
  • Is it ok to inject it like an autowired parameter in the Filter class? – Dejell Jun 03 '13 at 21:58
  • Yeah, you'll need to use field injection. This won't look like it makes sense, since the filter is instantiated once, but Jersey will use a thread-local proxy to make the injection happen per-request behind the scenes. – Paul Bellora Jun 03 '13 at 22:01
  • The links works, but not what is written inside. for instance https://jersey.dev.java.net/nonav/apidocs/1.1.2-ea/jersey/com/sun/jersey/spi/container/ContainerRequest.html – Dejell Jun 04 '13 at 05:30
  • 1
    Oh I see what you mean. Yeah, the Jersey documentation is often down and it's very annoying. I usually google the class name and then use a cached page. – Paul Bellora Jun 04 '13 at 13:21
  • I used to have my ResourceFilterFactory and inside it the ContainerRequestFilter. When I put @Context HttpServletRequest httpServletRequest; inside the ResourceFilterFactory it worked fine, but when I put it inside the ContainerRequestFilter it was null. Any reasons for why it would happen? – Dejell Jul 07 '13 at 06:23
  • @Odelya Are you using `new` to instantiate it yourself? Injection only works when Jersey does the instantiation. Otherwise I'm not sure. – Paul Bellora Jul 08 '13 at 05:35
  • @PaulBellora I'm using `@Provider public class ReqFilter implements ContainerRequestFilter { @Context private UriInfo uriInfo ...` to inject UriInfo.. I'm getting uriInfo.getPath but uriInfo.getPathParameters() is throwing null pointer.. the resource has PathParam.. Any idea, what can go wrong? – Kiran A B Mar 07 '16 at 16:19
0

2018 answer:

containerRequest.getUriInfo().getPathParameters()
Sridhar Sarnobat
  • 25,183
  • 12
  • 93
  • 106