如何使用UricomponentsBuilder替换程体

发布于 2025-02-08 10:10:47 字数 544 浏览 3 评论 0原文

我有一个URL,如下所示,

http://example.com/{id}

我想将其更改为

http://example.com/12

我的编码,

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);

因为它在其下方已将输出

http://example.com/%7Bid%7D/id/12

不确定是什么是使用UricomponentsBuilder的正确方法

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

Not sure what is right way to use UriComponentsBuilder

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨轻弹 2025-02-15 10:10:47

要用一个实际值替换占位符,您需要用参数调用构建

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

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

    System.out.println(uri);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文