Sachin Sachin - 23 days ago 9
Java Question

How to get path paramters from URL string in java

I want to create function which will take URL string as a parameter and return

MultivaluedHashMap<String, String>
as a output. I have format of url with me through which I can construct UriTemplate class.

So basically I want to create functionality similar to
requestContext.getUriInfo().getPathParameters()


So my function will be

public MultivaluedHashMap<String, String> getPathParamtersFromUrl(string url)
{
}


I am not sure how to get this.
Note : I don't want to extract query parameters. I want to extract path parameters.

Answer

I was able to figure out solution after closely looking at the UriTemplate class.

 private MultivaluedMap<String, String> getPathParameters(String uri, String strUrlTemplate) {

        UriTemplate template = new UriTemplate(strUrlTemplate);
        Map<String, String> parameters = new HashMap<>();
        Boolean value = template.match(uri, parameters);
        return new MultivaluedHashMap(parameters);

    } 
Comments