Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifying the port of a URI

Tags:

java

In Java, the URI class is immutable.

Here's how I'm currently modifying the port:

public URI uriWithPort(URI uri, int port) {
    try {
        return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), port,
                       uri.getPath(), uri.getQuery(), uri.getFragment());
    } catch (URISyntaxException e) {
        LOG.error("Updating URI port failed:",e);
        return uri;
    }
}

Is there a simpler way?

like image 258
Marty Pitt Avatar asked Jun 27 '11 07:06

Marty Pitt


1 Answers

You can also use URIBuider

http://download.oracle.com/javaee/6/api/javax/ws/rs/core/UriBuilder.html

UriBuilder.fromURI(uri).port(port).build("foo", "bar");
like image 60
Talha Ahmed Khan Avatar answered Oct 12 '22 11:10

Talha Ahmed Khan