目前,我正在使用字符串連接構建 url。String url = "http://localhost:8080/bo/" + docId;HttpEntity<String> httpEntity = this.handleJWT();restTemplate .exchange( url, HttpMethod.DELETE, httpEntity, Void.class );使用java構建rest url是否更優雅?
3 回答

泛舟湖上清波郎朗
TA貢獻1818條經驗 獲得超3個贊
您還可以使用UriComponentsBuiler
String url = "http://localhost:8080/bo/{docId}"
UriComponentsBuilder
.fromHttpUrl(url)
.buildAndExpand(docId)
.toUriString();
并且應該從屬性注入 url。

萬千封印
TA貢獻1891條經驗 獲得超3個贊
就在這里。當然不推薦像您這樣的硬編碼 URL。
Spring Boot Rest 允許您通過諸如@RequestMapping.
在您的情況下,帶@RestController注釋的類中的方法簽名可能是這樣的:
@RequestMapping(value = "/bo/{docId}", method = RequestMethod.DELETE)
public void request(@PathVariable("docId") int docId){
? ?...
}
例如,在瀏覽器中輸入 localhost:8080/bo/123 將導致一個方法調用,“123”是傳遞的參數。
使用此布局,您可以非常方便地觸發方法調用。
添加回答
舉報
0/150
提交
取消