首页 > 解决方案 > Spring boot:使用 ServletUriComponentsBuilder 构建一个 uri“/{id}”

问题描述

目前,我正在使用此代码片段来创建 uri 引用:

URI uri = ServletUriComponentsBuilder
    .fromCurrentContextPath()
    .path(EspaiDocConstants.Endpoints.DOWNLOAD)
    .path(attributes.getId())
    .build()
    .toUri();

但是,它会生成:

http://localhost:8080/downlo**adcp**d4-6b9f27c1-e9b3-4735-8127-8f18724734e9

中间**adcp**没有斜线!

我该如何解决?

标签: springspring-boot

解决方案


看起来path()只是连接字符串。您可以自己添加斜线

.path(EspaiDocConstants.Endpoints.DOWNLOAD)
.path("/")
.path(attributes.getId())

推荐阅读