介绍
可以通过URI的getPath()函数获取路径部分,通过getQuery()函数获取查询部分。
举例
有路径、有查询部分
package com.thb;
import java.net.URI;
import java.net.URISyntaxException;
public class Demo3 {
public static void main(String[] args) throws URISyntaxException {
String address = "http://localhost:8080/restful/result/?name=thb";
URI servicePoint = new URI(address);
String path = servicePoint.getPath();
String query = servicePoint.getQuery();
System.out.println("path: " + path);
System.out.println("query: " + query);
}
}
运行输出:
path: /restful/result/
query: name=thb
有路径、没有查询部分
package com.thb;
import java.net.URI;
import java.net.URISyntaxException;
public class Demo3 {
public static void main(String[] args) throws URISyntaxException {
String address = "http://localhost:8080/restful/result";
URI servicePoint = new URI(address);
String path = servicePoint.getPath();
String query = servicePoint.getQuery();
System.out.println("path: " + path);
System.out.println("query: " + query);
}
}
运行输出:
path: /restful/result
query: null