Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get the absolute request url without path parameters

Tags:

java

rest

jersey

I am trying to get the request url without values of path parameters into it.

Consider my complete url is

URl: http://localhost:8080/aaa/mock/abcd/1234/true
Path parameters: abcd, true
Output needed: /aaa/mock/abcd

My web service method looks like this.

@Path(value = "/aaa/mock")
@Component
public class MockService
{
    private static Log log = LogFactory.getLog(MockService.class);


    //address
    @GET
    @Path(value = "/{mockrequest}/{status}")
    @Produces(MediaType.JSON)
    public String mockEngagement(@Context ContainerRequestContext request,@PathParam("mockrequest") String mockrequest,@PathParam("status") String status )
    {
        log.info("The mock url is"+request.getUriInfo().getRequestUri());  
        log.info("The mock url is"+request.getUriInfo().getAbsolutePath()); 
        log.info("The mock url is"+request.getUriInfo().getBaseUri()); 
        log.info("The mock url is"+request.getUriInfo().getMatchedURIs()); 
        **//Out put needed /aaa/mock/abcd**
        return "ajaja";
    }


}

None of the above calls return the required info.

I am thinking if there is a generic process to get the desired output irrespective of number of path parameters.

Any such methods.

like image 327
Patan Avatar asked Nov 11 '22 11:11

Patan


1 Answers

Try UriInfo#getPath(), UriInfo#getPath(boolean), or UriInfo#getPathSegments(). The boolean argument is whether the path should be encoded or not.

https://jersey.java.net/apidocs/2.3.1/jersey/index.html

You could also get the absolute path and the base path and then use URI#relativize(URI).

like image 107
Eric Stein Avatar answered Nov 14 '22 22:11

Eric Stein