Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
kt-keystone
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Matrix
kt-keystone
Commits
6703c998
提交
6703c998
authored
1月 17, 2022
作者:
黄夏豪
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactor(base): 修复了代码中不规整的部分
上级
59e0ff01
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
54 行增加
和
38 行删除
+54
-38
HttpRequestDetail.java
...java/org/matrix/entity/httpRequest/HttpRequestDetail.java
+10
-16
HttpRequestType.java
...n/java/org/matrix/entity/httpRequest/HttpRequestType.java
+38
-8
RequestBody.java
.../main/java/org/matrix/entity/httpRequest/RequestBody.java
+2
-1
HttpClientUtil.java
kt-base/src/main/java/org/matrix/util/HttpClientUtil.java
+4
-13
没有找到文件。
kt-base/src/main/java/org/matrix/entity/httpRequest/HttpRequestDetail.java
浏览文件 @
6703c998
...
...
@@ -3,13 +3,14 @@ package org.matrix.entity.httpRequest;
import
io.swagger.annotations.ApiModelProperty
;
import
lombok.Data
;
import
org.apache.http.Header
;
import
org.apache.http.message.BasicHeader
;
import
org.springframework.http.HttpMethod
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.stream.Collectors
;
/**
* @author huangxiahao
*/
@Data
public
class
HttpRequestDetail
{
...
...
@@ -18,24 +19,23 @@ public class HttpRequestDetail {
private
List
<
RequestHeader
>
headers
=
new
ArrayList
<>();
@ApiModelProperty
(
"URL"
)
private
String
url
=
new
String
()
;
private
String
url
=
""
;
//
@ApiModelProperty("请求类型")
@ApiModelProperty
(
"请求类型"
)
private
HttpRequestType
requestType
=
HttpRequestType
.
NONE
;
@ApiModelProperty
(
"请求方法"
)
private
HttpMethod
method
;
@ApiModelProperty
(
"字符串入参"
)
private
String
stringValue
=
new
String
()
;
private
String
stringValue
=
""
;
@ApiModelProperty
(
"键值对入参"
)
private
List
<
RequestBody
>
requestBodies
=
new
ArrayList
<>();
/**
* 增加键值对入参
* @param requestBody
* @return
*/
private
Boolean
addRequestBody
(
RequestBody
requestBody
)
{
return
requestBodies
.
add
(
requestBody
);
...
...
@@ -43,22 +43,16 @@ public class HttpRequestDetail {
/**
* 增加请求头
* @param name
* @param value
* @return
*/
public
Boolean
addHeaders
(
String
name
,
String
value
)
{
return
headers
.
add
(
new
RequestHeader
(
name
,
value
));
public
void
addHeaders
(
String
name
,
String
value
)
{
headers
.
add
(
new
RequestHeader
(
name
,
value
));
}
/**
* 以数组的形式获取请求头
* @return
*/
public
Header
[]
getHeadersArray
()
{
List
<
BasicHeader
>
collect
=
headers
.
stream
().
map
(
RequestHeader:
:
getBasicHeader
).
collect
(
Collectors
.
toList
());
return
collect
.
toArray
(
new
Header
[
collect
.
size
()]);
return
headers
.
stream
().
map
(
RequestHeader:
:
getBasicHeader
).
toArray
(
Header
[]::
new
);
}
...
...
kt-base/src/main/java/org/matrix/entity/httpRequest/HttpRequestType.java
浏览文件 @
6703c998
...
...
@@ -5,26 +5,57 @@ import org.springframework.lang.Nullable;
import
java.util.HashMap
;
import
java.util.Map
;
/**
* @author huangxiahao
*/
public
enum
HttpRequestType
{
/**
* 通过将变量拼接在url中的方式请求,例如:127.0.0.1?a=1&b=2
*/
QUERY
,
REST
,
/**
* 通过FORM_DATA的方式发起请求
*/
FORM_DATA
,
/**
* 通过X_WWW_FORM_URLENCODED的方式发起请求
*/
X_WWW_FORM_URLENCODED
,
/**
* 通过TEXT发起请求
*/
TEXT
,
/**
* 通过JSON发起请求
*/
JSON
,
/**
* 通过XML发起请求
*/
XML
,
/**
* 通过BINARY发起请求
*/
BINARY
,
/**
* 无任何请求方式,空实现
*/
NONE
;
private
static
final
Map
<
String
,
HttpRequestType
>
mappings
=
new
HashMap
(
16
);
private
static
final
Map
<
String
,
HttpRequestType
>
MAPPINGS
=
new
HashMap
<>
(
16
);
private
HttpRequestType
()
{
}
@Nullable
public
static
HttpRequestType
resolve
(
@Nullable
String
method
)
{
return
method
!=
null
?
mappings
.
get
(
method
)
:
null
;
return
method
!=
null
?
MAPPINGS
.
get
(
method
)
:
null
;
}
public
boolean
matches
(
String
method
)
{
...
...
@@ -34,9 +65,8 @@ public enum HttpRequestType {
static
{
HttpRequestType
[]
var0
=
values
();
int
var1
=
var0
.
length
;
for
(
int
var2
=
0
;
var2
<
var1
;
++
var2
)
{
HttpRequestType
httpMethod
=
var0
[
var2
];
mappings
.
put
(
httpMethod
.
name
(),
httpMethod
);
for
(
HttpRequestType
httpMethod
:
var0
)
{
MAPPINGS
.
put
(
httpMethod
.
name
(),
httpMethod
);
}
}
...
...
kt-base/src/main/java/org/matrix/entity/httpRequest/RequestBody.java
浏览文件 @
6703c998
...
...
@@ -13,8 +13,9 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor
public
class
RequestBody
{
@ApiModelProperty
(
"键"
)
private
String
name
;
private
String
key
;
@ApiModelProperty
(
"参数类型,文件或者文本"
)
private
MultiPartRequestBodyType
type
;
...
...
kt-base/src/main/java/org/matrix/util/HttpClientUtil.java
浏览文件 @
6703c998
...
...
@@ -150,10 +150,10 @@ public class HttpClientUtil {
for
(
RequestBody
requestBody
:
requestBodies
)
{
switch
(
requestBody
.
getType
())
{
case
TEXT:
multipartEntityBuilder
.
addTextBody
(
requestBody
.
get
Name
(),
requestBody
.
getValue
(),
ContentType
.
create
(
"text/plain"
,
Consts
.
UTF_8
));
multipartEntityBuilder
.
addTextBody
(
requestBody
.
get
Key
(),
requestBody
.
getValue
(),
ContentType
.
create
(
"text/plain"
,
Consts
.
UTF_8
));
break
;
case
FILE:
multipartEntityBuilder
.
addBinaryBody
(
requestBody
.
get
Name
(),
getFileByPath
(
requestBody
.
getValue
()));
multipartEntityBuilder
.
addBinaryBody
(
requestBody
.
get
Key
(),
getFileByPath
(
requestBody
.
getValue
()));
break
;
default
:
}
...
...
@@ -164,7 +164,7 @@ public class HttpClientUtil {
public
HttpEntity
getUrlEncodeFormEntity
(
List
<
RequestBody
>
requestBodies
)
{
List
<
NameValuePair
>
param
=
new
ArrayList
<>();
for
(
RequestBody
requestBody
:
requestBodies
)
{
param
.
add
(
new
BasicNameValuePair
(
requestBody
.
get
Name
(),
requestBody
.
getValue
()));
param
.
add
(
new
BasicNameValuePair
(
requestBody
.
get
Key
(),
requestBody
.
getValue
()));
}
return
new
UrlEncodedFormEntity
(
param
,
StandardCharsets
.
UTF_8
);
}
...
...
@@ -203,15 +203,6 @@ public class HttpClientUtil {
String
url
=
httpRequestDetail
.
getUrl
();
try
{
switch
(
httpRequestDetail
.
getRequestType
())
{
case
REST:
String
regex
=
"(?<=\\{)(.*?)(?=})"
;
Pattern
pattern
=
Pattern
.
compile
(
regex
);
Matcher
mat
=
pattern
.
matcher
(
url
);
while
(
mat
.
find
())
{
url
=
url
.
replaceAll
(
"\\{"
+
mat
.
group
()
+
"}"
,
"1"
);
}
break
;
case
QUERY:
if
(
httpRequestDetail
.
getMethod
().
equals
(
HttpMethod
.
GET
))
{
URIBuilder
uriBuilder
;
...
...
@@ -219,7 +210,7 @@ public class HttpClientUtil {
for
(
RequestBody
requestBody
:
httpRequestDetail
.
getRequestBodies
())
{
switch
(
requestBody
.
getType
())
{
case
TEXT:
uriBuilder
.
setParameter
(
requestBody
.
get
Name
(),
requestBody
.
getValue
());
uriBuilder
.
setParameter
(
requestBody
.
get
Key
(),
requestBody
.
getValue
());
break
;
case
FILE:
throw
new
NullPointerException
(
"QUERY请求不能传入文件流"
);
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论