Construct or build URI

Straight up Java

Using Jersey implementation

Construct URI

@Test
public void construct_uri_jersey () {
              
    UriBuilder builder = UriBuilder
            .fromPath("www.leveluplunch.com")
            .scheme("http")
            .path("java/examples/");
    
    URI uri = builder.build();
    
    assertEquals(
            "http:/www.leveluplunch.com/java/examples/", 
            uri.toString());
}

Construct URI encoded

@Test
public void construct_uri_encoded_jersey () {

    UriBuilder builder = UriBuilder
            .fromPath("www.leveluplunch.com")
            .scheme("http")
            .path("java/examples/?sample=uri encode");
    
    URI uri = builder.build();
    
    assertEquals(
            "http:/www.leveluplunch.com/java/examples/%3Fsample=uri%20encode", 
            uri.toString());
}

Construct URI w/ template

@Test
public void construct_uri_template_jersey () {
    
    UriBuilder builder = UriBuilder
            .fromPath("www.leveluplunch.com")
            .path("/{lanuage}/{type}/");
    
    URI uri = builder.build("java", "examples");
    
    assertEquals(
            "www.leveluplunch.com/java/examples/", 
            uri.toString());
}

Construct URI w/ query parameter

@Test
public void construct_uri_parameter_jersey () {

    UriBuilder builder = UriBuilder
            .fromPath("www.leveluplunch.com")
            .path("/{lanuage}/{type}/")
            .queryParam("test", "a", "b");
    
    URI uri = builder.build("java", "examples");
    
    assertEquals(
            "www.leveluplunch.com/java/examples/?test=a&test=b", 
            uri.toString());
}

Google Guava

Guava currently doesn't have support for a URI builder utility and there is an accepted feature request in the issues log.

Apache Commons

Apache commons with httpclient

Construct URI

@Test
public void contruct_uri_apache () throws URISyntaxException {
    
    URIBuilder builder = new URIBuilder()
            .setScheme("http")
            .setHost("www.leveluplunch.com")
            .setPath("/java/examples/");
    
    URI uri = builder.build();
    
    assertEquals(
            "http://www.leveluplunch.com/java/examples/", 
            uri.toString());
}

Construct URI encoded

@Test
public void contruct_uri_encoded_apache () throws URISyntaxException {
    
    URIBuilder builder = new URIBuilder()
            .setScheme("http")
            .setHost("www.leveluplunch.com")
            .setPath("/java/examples/?sample=uri encode");
    
    URI uri = builder.build();
    
    assertEquals(
            "http://www.leveluplunch.com/java/examples/%3Fsample=uri%20encode", 
            uri.toString());
}

Construct URI w/ query parameter

@Test
public void contruct_uri_parameters_apache () throws URISyntaxException {
    
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
    nameValuePairs.add(new BasicNameValuePair("test","a"));
    nameValuePairs.add(new BasicNameValuePair("test","b"));
      
    URIBuilder builder = new URIBuilder()
            .setScheme("http")
            .setHost("www.leveluplunch.com")
            .setParameters(nameValuePairs);
    
    URI uri = builder.build();
    
    assertEquals(
            "http://www.leveluplunch.com?test=a&test=b", 
            uri.toString());
}

Spring Framework

UriComponents below is from the org.springframework.web.util package. There is also a URIBuilder in spring social.

Construct URI

@Test
public void construct_uri_spring () {
    
    UriComponents uriComponents =
            UriComponentsBuilder.newInstance()
                .scheme("http").host("www.leveluplunch.com").path("/java/examples/")
                .build();
    
    assertEquals("http://www.leveluplunch.com/java/examples/", uriComponents.toUriString());
}

Construct URI encoded

@Test
public void construct_uri_encoded_spring () {
    
    UriComponents uriComponents =
            UriComponentsBuilder.newInstance()
                .scheme("http").host("www.leveluplunch.com")
                .path("/java/examples/?sample=uri encode")
                .build()
                .encode();
    
    assertEquals("http://www.leveluplunch.com/java/examples/%3Fsample=uri%20encode", 
            uriComponents.toUriString());
}

Construct URI w/ template

@Test
public void construct_uri_template_spring () {
    
    UriComponents uriComponents =
            UriComponentsBuilder.newInstance()
                .scheme("http")
                .host("www.leveluplunch.com")
                .path("/{lanuage}/{type}/")
                .build()
                .expand("java", "examples")
                .encode();
    
    assertEquals("http://www.leveluplunch.com/java/examples/", 
            uriComponents.toUriString());
}

Construct URI w/ query parameter

@Test
public void construct_uri_queryparmeter_spring () {
    
    UriComponents uriComponents =
            UriComponentsBuilder.newInstance()
                .scheme("http")
                .host("www.leveluplunch.com")
                .path("/{lanuage}/{type}/")
                .queryParam("test", "a", "b")
                .build()
                .expand("java", "examples")
                .encode();
    
    assertEquals("http://www.leveluplunch.com/java/examples/?test=a&test=b", 
            uriComponents.toUriString());
}