Search code examples
springrelative-pathurl-encodingquery-string

UriComponentsBuilder query param and fragment


I want to generate the following path: '/app#/fragment1?test=toto' with spring library UriComponentsBuilder.

What I have tried so far:

UriComponentsBuilder ucb = UriComponentsBuilder.fromPath("/app").fragment("fragment1").queryParam("test","toto");

ucb.toUriString(); // -> gives me as result : '/app?test=toto#/fragment1'

Any idea how to achieve this in an elegant way?


Solution

  • I would simply do something like :

    // first build fragment part
    String fragmentWithQueryParams = UriComponentsBuilder.fromPath("/fragment1").queryParam("test","toto").toUriString();
    
    // then generate full path
    String fullPath = UriComponentsBuilder.fromPath("/app").fragment(fragmentWithQueryParams).toUriString());
    
    System.out.println(fullPath); // -> "/app#/fragment1?test=toto"