Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to use UriComponentsBuilder for replacing pathsegment

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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/%7Bid%7D/id/12

Not sure what is right way to use UriComponentsBuilder

>Solution :

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading