users@jersey.java.net

[Jersey] Re: How to get matched path in jersey 1.0

From: Miroslav Fuksa <miroslav.fuksa_at_oracle.com>
Date: Thu, 14 Aug 2014 11:31:14 +0200

Hi,

I wrote an answer for Jersey 2.x and then I notices you have in the email subject “jersey 1.0”. So, this is the answer for Jersey 2.x:

for such a purpose like a logging I would use Monitoring support in Jersey [1]. You can register the ApplicationEventListener which would return an implementation of RequestEventListener from onRequest() method. Your RequestEventListener implementation will log on event type RESOURCE_METHOD_START. You can get the UriInfo by RequestEvent.getUriInfo(). UriInfo contains information about path parameters.

Another option is to use ContainerRequestFilter (but it must NOT be @Prematching filter). There you can access UriInfo too (ContainerRequestContext.getUriInfo()). If you need to log only this case then the filter is fine. If you decide to log more events (like exception mappers, responses) the monitoring support described above is more suitable I would say (you can hook to many places of request processing)

For Jersey 1.x you can use the solution with ContainerRequestFilter.

I hope this helps.
Mira


[1]: jersey monitoring documentation https://jersey.java.net/documentation/latest/monitoring_tracing.html#monitoring

On Aug 14, 2014, at 7:38 AM, Magesh STM <magesh_stm_at_yahoo.com> wrote:

> I have this following question. Please help.
>
> Please refer following code. /hello/from/JohnDoe will hit the method sayPlainTextHello.
>
> When "/hello/from/JohnDoe" is accessed, I want to store the matched path which is /hello/from/{name} in a log. Please note that I can't modify below code but can add filter, etc. to the app. How to get the matched path "/hello/from/{name}" ?
>
> @Path("hello")
> public class SayHello {
>
> @GET
> @Produces(MediaType.TEXT_PLAIN)
> @Path("/from/{name}")
> public String sayPlainTextHello(@PathParam("name") String fromName) {
> return "Hello Jersey - " + fromName;
> }
> }