This commit is contained in:
Looly 2021-10-26 01:33:30 +08:00
parent f88e7ab2f3
commit 87b88b395e
4 changed files with 49 additions and 17 deletions

View File

@ -3,7 +3,7 @@
-------------------------------------------------------------------------------------------------------------
# 5.7.16 (2021-10-24)
# 5.7.16 (2021-10-26)
### 🐣新特性
* 【core 】 增加DateTime.toLocalDateTime
@ -13,6 +13,7 @@
* 【core 】 NumberUtil.compare修正注释说明issue#I4FAJ1@Gitee
### 🐞Bug修复
* 【core 】 修复UrlBuilder.addPath歧义问题issue#1912@Github
-------------------------------------------------------------------------------------------------------------

View File

@ -322,13 +322,25 @@ public final class UrlBuilder implements Serializable {
}
/**
* 增加路径节点
* 增加路径在现有路径基础上追加路径
*
* @param path 路径例如aaa/bbb/ccc
* @return this
*/
public UrlBuilder addPath(CharSequence path) {
UrlPath.of(path, this.charset).getSegments().forEach(this::addPathSegment);
return this;
}
/**
* 增加路径节点路径节点中的"/"会被转义为"%2F"
*
* @param segment 路径节点
* @return this
* @since 5.7.16
*/
public UrlBuilder addPath(String segment) {
if (StrUtil.isBlank(segment)) {
public UrlBuilder addPathSegment(CharSequence segment) {
if (StrUtil.isEmpty(segment)) {
return this;
}
if (null == this.path) {
@ -341,19 +353,13 @@ public final class UrlBuilder implements Serializable {
/**
* 追加path节点
*
* @param segment path节点
* @param path path节点
* @return this
* @deprecated 方法重复请使用{@link #addPath(CharSequence)}
*/
public UrlBuilder appendPath(CharSequence segment) {
if (StrUtil.isEmpty(segment)) {
return this;
}
if (this.path == null) {
this.path = new UrlPath();
}
this.path.add(segment);
return this;
@Deprecated
public UrlBuilder appendPath(CharSequence path) {
return addPath(path);
}
/**

View File

@ -29,7 +29,7 @@ public class UrlPath {
* @param charset decode用的编码null表示不做decode
* @return UrlPath
*/
public static UrlPath of(String pathStr, Charset charset) {
public static UrlPath of(CharSequence pathStr, Charset charset) {
final UrlPath urlPath = new UrlPath();
urlPath.parse(pathStr, charset);
return urlPath;
@ -97,7 +97,7 @@ public class UrlPath {
* @param charset decode编码null表示不解码
* @return this
*/
public UrlPath parse(String path, Charset charset) {
public UrlPath parse(CharSequence path, Charset charset) {
if (StrUtil.isNotEmpty(path)) {
// 原URL中以/结尾则这个规则需保留issue#I1G44J@Gitee
if(StrUtil.endWith(path, CharUtil.SLASH)){

View File

@ -281,4 +281,29 @@ public class UrlBuilderTest {
final UrlBuilder urlBuilder = UrlBuilder.ofHttp(url);
Assert.assertEquals(url, urlBuilder.toString());
}
@Test
public void addPathEncodeTest(){
String url = UrlBuilder.create()
.setScheme("https")
.setHost("domain.cn")
.addPath("api")
.addPath("xxx")
.addPath("bbb")
.build();
Assert.assertEquals("https://domain.cn/api/xxx/bbb", url);
}
@Test
public void addPathEncodeTest2(){
// https://github.com/dromara/hutool/issues/1912
String url = UrlBuilder.create()
.setScheme("https")
.setHost("domain.cn")
.addPath("/api/xxx/bbb")
.build();
Assert.assertEquals("https://domain.cn/api/xxx/bbb", url);
}
}