Search code examples
javauribuilder

How add a trailing slash after the port in apache URIBuilder?


I am trying to build a URI having a trailing slash but can't happen to find the way to do it with URIBuilder. Is that even possible?

Expected result would be: http://test.com:24/?param1=value1&param2=value2

@Test
public void ttt(){
    final URIBuilder uriBuilder = new URIBuilder();
    uriBuilder.setScheme("http");
    uriBuilder.setHost("test.com");
    uriBuilder.setPort(24);
    uriBuilder.addParameter("param1", "value1");
    uriBuilder.addParameter("param2", "value2");
    System.out.println(uriBuilder.toString());
}

What I get at the moment: http://test.com:24?param1=value1&param2=value2

Any idea?


Solution

  • There is setPath(str) method, see URIBuilder.setPath(String)

    So your code should look like

    @Test
    public void ttt(){
        final URIBuilder uriBuilder = new URIBuilder();
        uriBuilder.setScheme("http");
        uriBuilder.setHost("test.com");
        uriBuilder.setPort(24);
        uriBuilder.setPath("/");
        uriBuilder.addParameter("param1", "value1");
        uriBuilder.addParameter("param2", "value2");
        System.out.println(uriBuilder.toString());
    }
    

    Output

    http://test.com:24/?param1=value1&param2=value2