Sachin Sachin - 1 year ago 98
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

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 Source

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);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download