Home > Blockchain >  how to use UriComponentsBuilder for replacing pathsegment
how to use UriComponentsBuilder for replacing pathsegment

Time:06-16

I have a url as below

http://example.com/{id}

I want to change it to

http://example.com/12

I have coded as below

url= "http://example.com/{id}";

UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(url);
builder.pathSegment("{id}", "12");
UriComponents uriComponents = builder.build().encode();
URI uri = uriComponents.toUri();

System.out.println(uri);

Its giving me output as

http://example.com/{id}/id/12

Not sure what is right way to use UriComponentsBuilder

CodePudding user response:

To replace placeholder with a real value, you need to call build with parameters

    String url = "http://example.com/{id}";

    UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(url);
    URI uri = builder.build("12");

    System.out.println(uri);
  • Related