| 知乎专栏 |
UriComponents https = UriComponentsBuilder.newInstance()
.scheme("https")
.host("www.netkiller.cn")
.port("8080")
.path("/article")
.queryParam("id", "9527")
.encode(StandardCharsets.UTF_8)
.build();
log.info(https.toUriString());
URL 解析
String httpUrl = "https://www.netkiller.cn:8080/article?id=9527";
UriComponents uriComponents = UriComponentsBuilder.fromHttpUrl(httpUrl).build();
提取协议头
String scheme = uriComponents.getScheme();
// scheme = https
System.out.println("scheme = " + scheme);
获取host操作。
String host = uriComponents.getHost();
// host = felord.cn
System.out.println("host = " + host);
提取 Port 端口。
int port = uriComponents.getPort();
// port = -1
System.out.println("port = " + port);
但是很奇怪的是上面的是 -1,很多人误以为会是80。其实 Http 协议确实是80,但是java.net.URL#getPort()规定,若 URL 的实例未申明(省略)端口号,则返回值为-1。所以当返回了-1就等同于80,但是 URL 中不直接体现它们。
提取 Path 路径
String path = uriComponents.getPath();
// path = /spring-security/{article}
System.out.println("path = " + path);
提取 Query 参数
String query = uriComponents.getQuery();
// query = version=1×tamp=123123325
System.out.println("query = " + query);
更加合理的提取方式:
MultiValueMap<String, String> queryParams = uriComponents.getQueryParams();
// queryParams = {version=[1], timestamp=[123123325]}
System.out.println("queryParams = " + queryParams);
fromPath
String endpoint = "/employees";
UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromPath(endpoint)
.queryParam("param1", "value1")
.queryParam("param2", "value2");
webClient.post()
.uri(uriBuilder.build().toUri())
.bodyValue(new Employee(...))
.retrieve()
.bodyToMono(Employee.class);
替换变量
UriComponents uriComponents = UriComponentsBuilder.newInstance()
.scheme("https")
.host("www.netkiller.cn")
.port("8080")
.path("/article/{category}")
.queryParam("id", "9527")
.encode(StandardCharsets.UTF_8)
.build();
UriComponents expand = uriComponents.expand("story");
log.info(expand.toUriString());
# https://www.netkiller.cn:8080/article/story?id=9527
UriComponents uriComponents = UriComponentsBuilder.newInstance()
.scheme("https")
.host("www.netkiller.cn")
.port("8080")
.path("/book/{chapter}/{section}")
.queryParam("id", "9527")
.encode(StandardCharsets.UTF_8)
.build();
UriComponents expand = uriComponents.expand(Map.of("chapter", "chapter1", "section", "section2"));
log.info(expand.toUriString());
# https://www.netkiller.cn:8080/book/chapter1/section2?id=9527
fragment
import org.springframework.web.util.UriComponentsBuilder;
public class UriBuilderExample {
public static void main(String[] args) {
// Base URI
String baseUri = "https://www.example.com";
// 构建 URI
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(baseUri);
// 添加路径变量: https://www.example.com/users/{userId}
builder.path("/users/{userId}");
// 添加查询参数: https://www.example.com/users/{userId}?name=John&age=30
builder.queryParam("name", "John")
.queryParam("age", 30);
// 添加片段,https://www.example.com/users/{userId}?name=John&age=30#profile
builder.fragment("profile");
// 构建最终的 URI,替换 url 中的路径变量:https://www.example.com/users/123?name=John&age=30#profile
String finalUri = builder.buildAndExpand("123").toUriString();
System.out.println("Final URI: " + finalUri);
}
}