除了基本的参数拼接,UriComponentsBuilder在构建RESTful API的
路径变量时有哪些常用方法?
1. 基本路径变量替换
简单路径变量
// 单个路径变量
String url = UriComponentsBuilder
.fromHttpUrl("http://api.example.com/")
.path("/users/{userId}")
.buildAndExpand(12345)
.toUriString();
// 结果: http://api.example.com/users/12345
多个路径变量
// 多个路径变量
String url = UriComponentsBuilder
.fromHttpUrl("http://api.example.com/")
.path("/organizations/{orgId}/departments/{deptId}/employees/{empId}")
.buildAndExpand("acme", 101, 5001)
.toUriString();
// 结果: http://api.example.com/organizations/acme/departments/101/employees/5001
2. Map形式提供路径变量值