提交 e653fd05 authored 作者: 黄夏豪's avatar 黄夏豪

refactor(base): 修复了代码中不贵珍的部分

上级 05393b19
...@@ -24,13 +24,7 @@ ...@@ -24,13 +24,7 @@
<version>1.2.39</version> <version>1.2.39</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
<!-- httpClient --> <!-- httpClient -->
<dependency> <dependency>
...@@ -91,13 +85,6 @@ ...@@ -91,13 +85,6 @@
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
...@@ -146,13 +133,7 @@ ...@@ -146,13 +133,7 @@
<version>1.2.47</version> <version>1.2.47</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
<dependency> <dependency>
<groupId>com.relevantcodes</groupId> <groupId>com.relevantcodes</groupId>
......
package org.matrix.entity.httpRequest; package org.matrix.entity.httpRequest;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
/**
* @author huangxiahao
*/
@Data @Data
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
public class RequestBody { public class RequestBody {
// @ApiModelProperty("键") @ApiModelProperty("键")
private String name; private String name;
// @ApiModelProperty("参数类型,文件或者文本") @ApiModelProperty("参数类型,文件或者文本")
private MultiPartRequestBodyType type; private MultiPartRequestBodyType type;
// @ApiModelProperty("值") @ApiModelProperty("值")
private String value; private String value;
} }
...@@ -24,7 +24,6 @@ import org.springframework.http.HttpMethod; ...@@ -24,7 +24,6 @@ import org.springframework.http.HttpMethod;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.KeyManagementException; import java.security.KeyManagementException;
...@@ -40,9 +39,6 @@ import java.util.regex.Pattern; ...@@ -40,9 +39,6 @@ import java.util.regex.Pattern;
* HttpClient调用封装 * HttpClient调用封装
* *
* @author HuangXiahao * @author HuangXiahao
* @version V1.0
* @class AuthenticationUtils
* @packageName com.example.personnelmanager.common.utils
**/ **/
public class HttpClientUtil { public class HttpClientUtil {
...@@ -65,34 +61,28 @@ public class HttpClientUtil { ...@@ -65,34 +61,28 @@ public class HttpClientUtil {
/** /**
* 初始化请求内数据并补全动态变量 * 初始化请求内数据并补全动态变量
* @param httpRequestDetail
*/ */
public void execute(HttpRequestDetail httpRequestDetail){ public void completeHttpRequestDetail(HttpRequestDetail httpRequestDetail){
for (RequestHeader header : httpRequestDetail.getHeaders()) { for (RequestHeader header : httpRequestDetail.getHeaders()) {
//获取动态变量,并将动态变量替换进去 //获取动态变量,并将动态变量替换进去
//todo //todo 李迪凡 将header中的key和value里的 动态变量补全
// header.setValue();
} }
for (RequestBody requestBody : httpRequestDetail.getRequestBodies()) { for (RequestBody requestBody : httpRequestDetail.getRequestBodies()) {
//todo //todo 李迪凡 将requestBody中的key和value里的 动态变量补全
} }
if (StringUtils.isEmpty(httpRequestDetail.getStringValue())){ if (StringUtils.isEmpty(httpRequestDetail.getStringValue())){
//todo //todo 李迪凡 将httpRequestDetail的stringValue 动态变量补全
} }
//todo //todo 李迪凡 将httpRequestDetail的url 动态变量补全
// httpRequestDetail.setUrl();
} }
/** /**
* 发起Http请求 * 发起Http请求
* *
* @param httpRequestDetail
* @return
*/ */
private HttpResponseDetail sendHttpRequest(HttpRequestDetail httpRequestDetail) { private HttpResponseDetail sendHttpRequest(HttpRequestDetail httpRequestDetail) {
completeHttpRequestDetail(httpRequestDetail);
CloseableHttpResponse response = null; CloseableHttpResponse response = null;
Date startTime = new Date(); Date startTime = new Date();
String url = initUrlString(httpRequestDetail); String url = initUrlString(httpRequestDetail);
...@@ -125,11 +115,9 @@ public class HttpClientUtil { ...@@ -125,11 +115,9 @@ public class HttpClientUtil {
/** /**
* 初始化请求HttpEntity * 初始化请求HttpEntity
* *
* @param httpRequestDetail
* @return
*/ */
public HttpEntity initHttpEntity(HttpRequestDetail httpRequestDetail) { public HttpEntity initHttpEntity(HttpRequestDetail httpRequestDetail) {
HttpEntity httpEntity = null; HttpEntity httpEntity ;
switch (httpRequestDetail.getRequestType()) { switch (httpRequestDetail.getRequestType()) {
case FORM_DATA: case FORM_DATA:
httpEntity = getMultipartEntity(httpRequestDetail.getRequestBodies()); httpEntity = getMultipartEntity(httpRequestDetail.getRequestBodies());
...@@ -167,6 +155,7 @@ public class HttpClientUtil { ...@@ -167,6 +155,7 @@ public class HttpClientUtil {
case FILE: case FILE:
multipartEntityBuilder.addBinaryBody(requestBody.getName(), getFileByPath(requestBody.getValue())); multipartEntityBuilder.addBinaryBody(requestBody.getName(), getFileByPath(requestBody.getValue()));
break; break;
default:
} }
} }
return multipartEntityBuilder.build(); return multipartEntityBuilder.build();
...@@ -182,7 +171,7 @@ public class HttpClientUtil { ...@@ -182,7 +171,7 @@ public class HttpClientUtil {
public StringEntity getEmptyStringEntity() { public StringEntity getEmptyStringEntity() {
return new StringEntity(new String(), StandardCharsets.UTF_8); return new StringEntity("", StandardCharsets.UTF_8);
} }
public StringEntity getStringEntity(String value) { public StringEntity getStringEntity(String value) {
...@@ -199,7 +188,7 @@ public class HttpClientUtil { ...@@ -199,7 +188,7 @@ public class HttpClientUtil {
File file = getFileByPath(path); File file = getFileByPath(path);
byte[] bytesArray = new byte[(int) file.length()]; byte[] bytesArray = new byte[(int) file.length()];
FileInputStream fis = new FileInputStream(file); FileInputStream fis = new FileInputStream(file);
fis.read(bytesArray); // read file into bytes[] fis.read(bytesArray);
fis.close(); fis.close();
return new ByteArrayEntity(bytesArray); return new ByteArrayEntity(bytesArray);
} catch (IOException ioException) { } catch (IOException ioException) {
...@@ -215,16 +204,16 @@ public class HttpClientUtil { ...@@ -215,16 +204,16 @@ public class HttpClientUtil {
try { try {
switch (httpRequestDetail.getRequestType()) { switch (httpRequestDetail.getRequestType()) {
case REST: case REST:
String regex = "(?<=\\{)(.*?)(?=\\})"; String regex = "(?<=\\{)(.*?)(?=})";
Pattern pattern = Pattern.compile(regex); Pattern pattern = Pattern.compile(regex);
Matcher mat = pattern.matcher(url); Matcher mat = pattern.matcher(url);
while (mat.find()) { while (mat.find()) {
url = url.replaceAll("\\{" + mat.group() + "\\}", "1"); url = url.replaceAll("\\{" + mat.group() + "}", "1");
} }
break; break;
case QUERY: case QUERY:
if (httpRequestDetail.getMethod().equals(HttpMethod.GET)) { if (httpRequestDetail.getMethod().equals(HttpMethod.GET)) {
URIBuilder uriBuilder = null; URIBuilder uriBuilder ;
uriBuilder = new URIBuilder(url); uriBuilder = new URIBuilder(url);
for (RequestBody requestBody : httpRequestDetail.getRequestBodies()) { for (RequestBody requestBody : httpRequestDetail.getRequestBodies()) {
switch (requestBody.getType()) { switch (requestBody.getType()) {
...@@ -233,11 +222,13 @@ public class HttpClientUtil { ...@@ -233,11 +222,13 @@ public class HttpClientUtil {
break; break;
case FILE: case FILE:
throw new NullPointerException("QUERY请求不能传入文件流"); throw new NullPointerException("QUERY请求不能传入文件流");
default:
} }
} }
url = uriBuilder.build().toString(); url = uriBuilder.build().toString();
} }
break; break;
default:
} }
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
e.printStackTrace(); e.printStackTrace();
...@@ -248,9 +239,6 @@ public class HttpClientUtil { ...@@ -248,9 +239,6 @@ public class HttpClientUtil {
/** /**
* 初始化HttpRequestBase * 初始化HttpRequestBase
* *
* @param url
* @param httpMethod
* @return
*/ */
public HttpRequestBase initHttpRequestBase(String url, HttpMethod httpMethod) { public HttpRequestBase initHttpRequestBase(String url, HttpMethod httpMethod) {
HttpRequestBase requestBase; HttpRequestBase requestBase;
...@@ -276,28 +264,9 @@ public class HttpClientUtil { ...@@ -276,28 +264,9 @@ public class HttpClientUtil {
return requestBase; return requestBase;
} }
public static void main(String[] args) throws MalformedURLException, URISyntaxException {
// JSONObject jsonObject = new JSONObject();
// jsonObject.put("method","POST");
// Vo vo = JSON.parseObject(jsonObject.toJSONString(), Vo.class);
// System.out.println("1");
// String regex = "\\{([^\\}]+)\\}";
HttpRequestDetail httpRequestDetail = new HttpRequestDetail();
httpRequestDetail.getRequestBodies().add(new RequestBody("name", MultiPartRequestBodyType.TEXT, "张三"));
httpRequestDetail.getRequestBodies().add(new RequestBody("text", MultiPartRequestBodyType.TEXT, "a1"));
httpRequestDetail.setMethod(HttpMethod.GET);
httpRequestDetail.setUrl("www.baidu.com");
httpRequestDetail.setRequestType(HttpRequestType.QUERY);
}
/** /**
* 关闭response(注意:用完记得关,不然会一直占用系统资源) * 关闭response(注意:用完记得关,不然会一直占用系统资源)
* *
* @param response
*/ */
public void closeResponse(CloseableHttpResponse response) { public void closeResponse(CloseableHttpResponse response) {
if (response != null) { if (response != null) {
...@@ -315,7 +284,6 @@ public class HttpClientUtil { ...@@ -315,7 +284,6 @@ public class HttpClientUtil {
/** /**
* 关闭requestBase(注意:用完记得关,不然会一直占用系统资源) * 关闭requestBase(注意:用完记得关,不然会一直占用系统资源)
* *
* @param requestBase
*/ */
public void closeRequest(HttpRequestBase requestBase) { public void closeRequest(HttpRequestBase requestBase) {
requestBase.abort(); requestBase.abort();
......
...@@ -12,6 +12,11 @@ ...@@ -12,6 +12,11 @@
<artifactId>kt-kit</artifactId> <artifactId>kt-kit</artifactId>
<dependencies> <dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
</dependencies> </dependencies>
</project> </project>
\ No newline at end of file
...@@ -6,6 +6,7 @@ import lombok.Data; ...@@ -6,6 +6,7 @@ import lombok.Data;
* Hello world! * Hello world!
* *
*/ */
@Data
public class App public class App
{ {
public static void main( String[] args ) public static void main( String[] args )
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论