From 6e8966246e686f9ede8ba5e434d1fd10a37f2955 Mon Sep 17 00:00:00 2001
From: wangyu <727842003@qq.com>
Date: Thu, 13 Apr 2023 09:30:50 +0800
Subject: [PATCH] =?UTF-8?q?feat:=20=E5=8D=87=E7=BA=A71.1.2=EF=BC=8C?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=A0=87=E5=87=86=E6=B3=A8=E8=A7=A3?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
pom.xml | 4 +-
rest-proxy-api/pom.xml | 2 +-
.../flyfish/rest/annotation/RestApi.java | 16 ++---
.../rest/annotation/methods/DELETE.java | 61 +++++++++++++++++
.../flyfish/rest/annotation/methods/GET.java | 60 +++++++++++++++++
.../rest/annotation/methods/PATCH.java | 67 +++++++++++++++++++
.../flyfish/rest/annotation/methods/POST.java | 67 +++++++++++++++++++
.../flyfish/rest/annotation/methods/PUT.java | 67 +++++++++++++++++++
rest-proxy-core/pom.xml | 2 +-
.../flyfish/rest/constants/RestConstants.java | 2 +
.../rest/core/client/DefaultRestClient.java | 3 +
.../group/flyfish/rest/MultipartTest.java | 10 +--
.../group/flyfish/rest/RestProxyTest.java | 12 ++--
.../group/flyfish/rest/SimpleProxyTest.java | 5 +-
14 files changed, 355 insertions(+), 23 deletions(-)
create mode 100644 rest-proxy-api/src/main/java/group/flyfish/rest/annotation/methods/DELETE.java
create mode 100644 rest-proxy-api/src/main/java/group/flyfish/rest/annotation/methods/GET.java
create mode 100644 rest-proxy-api/src/main/java/group/flyfish/rest/annotation/methods/PATCH.java
create mode 100644 rest-proxy-api/src/main/java/group/flyfish/rest/annotation/methods/POST.java
create mode 100644 rest-proxy-api/src/main/java/group/flyfish/rest/annotation/methods/PUT.java
diff --git a/pom.xml b/pom.xml
index 53d2c17..a8dd3d9 100644
--- a/pom.xml
+++ b/pom.xml
@@ -13,7 +13,7 @@
group.flyfish
rest-proxy
- 1.1.1
+ 1.1.2
8
@@ -22,7 +22,7 @@
1.8
4.4
2.6
- 1.1.1
+ 1.1.2
pom
diff --git a/rest-proxy-api/pom.xml b/rest-proxy-api/pom.xml
index 12e66da..3412ecd 100644
--- a/rest-proxy-api/pom.xml
+++ b/rest-proxy-api/pom.xml
@@ -5,7 +5,7 @@
group.flyfish
rest-proxy
- 1.1.1
+ 1.1.2
4.0.0
diff --git a/rest-proxy-api/src/main/java/group/flyfish/rest/annotation/RestApi.java b/rest-proxy-api/src/main/java/group/flyfish/rest/annotation/RestApi.java
index f9cacd2..d98020b 100644
--- a/rest-proxy-api/src/main/java/group/flyfish/rest/annotation/RestApi.java
+++ b/rest-proxy-api/src/main/java/group/flyfish/rest/annotation/RestApi.java
@@ -24,6 +24,14 @@ public @interface RestApi {
@AliasFor("uri")
String value() default "";
+ /**
+ * 请求uri,使用次标注必须指定BaseUrl或者配置(现在还不支持)
+ *
+ * @return uri
+ */
+ @AliasFor("value")
+ String uri() default "";
+
/**
* 请求方法
*
@@ -45,14 +53,6 @@ public @interface RestApi {
*/
String url() default "";
- /**
- * 请求uri,使用次标注必须指定BaseUrl或者配置(现在还不支持)
- *
- * @return uri
- */
- @AliasFor("value")
- String uri() default "";
-
/**
* 基本路径,包含host
*
diff --git a/rest-proxy-api/src/main/java/group/flyfish/rest/annotation/methods/DELETE.java b/rest-proxy-api/src/main/java/group/flyfish/rest/annotation/methods/DELETE.java
new file mode 100644
index 0000000..013b9d8
--- /dev/null
+++ b/rest-proxy-api/src/main/java/group/flyfish/rest/annotation/methods/DELETE.java
@@ -0,0 +1,61 @@
+package group.flyfish.rest.annotation.methods;
+
+
+import group.flyfish.rest.annotation.RestApi;
+import group.flyfish.rest.enums.HttpMethod;
+import org.springframework.core.annotation.AliasFor;
+
+import java.lang.annotation.*;
+
+/**
+ * 标准的DELETE声明
+ *
+ * @author wangyu
+ */
+@Target(ElementType.METHOD)
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+@RestApi(method = HttpMethod.DELETE)
+public @interface DELETE {
+
+ /**
+ * uri的别名
+ *
+ * @return 结果
+ */
+ @AliasFor(annotation = RestApi.class)
+ String value() default "";
+
+ /**
+ * 请求uri,使用次标注必须指定BaseUrl或者配置(现在还不支持)
+ *
+ * @return uri
+ */
+ @AliasFor(annotation = RestApi.class)
+ String uri() default "";
+
+ /**
+ * 可选指定的url,不会从默认地址请求
+ *
+ * @return url
+ */
+ @AliasFor(annotation = RestApi.class)
+ String url() default "";
+
+
+ /**
+ * 基本路径,包含host
+ *
+ * @return baseUrl
+ */
+ @AliasFor(annotation = RestApi.class)
+ String baseUrl() default "";
+
+ /**
+ * 是否带上认证token
+ *
+ * @return 结果
+ */
+ @AliasFor(annotation = RestApi.class)
+ boolean credentials() default false;
+}
diff --git a/rest-proxy-api/src/main/java/group/flyfish/rest/annotation/methods/GET.java b/rest-proxy-api/src/main/java/group/flyfish/rest/annotation/methods/GET.java
new file mode 100644
index 0000000..1860c4f
--- /dev/null
+++ b/rest-proxy-api/src/main/java/group/flyfish/rest/annotation/methods/GET.java
@@ -0,0 +1,60 @@
+package group.flyfish.rest.annotation.methods;
+
+
+import group.flyfish.rest.annotation.RestApi;
+import group.flyfish.rest.enums.HttpMethod;
+import org.springframework.core.annotation.AliasFor;
+
+import java.lang.annotation.*;
+
+/**
+ * 标准的GET声明
+ * @author wangyu
+ */
+@Target(ElementType.METHOD)
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+@RestApi(method = HttpMethod.GET)
+public @interface GET {
+
+ /**
+ * uri的别名
+ *
+ * @return 结果
+ */
+ @AliasFor(annotation = RestApi.class)
+ String value() default "";
+
+ /**
+ * 请求uri,使用次标注必须指定BaseUrl或者配置(现在还不支持)
+ *
+ * @return uri
+ */
+ @AliasFor(annotation = RestApi.class)
+ String uri() default "";
+
+ /**
+ * 可选指定的url,不会从默认地址请求
+ *
+ * @return url
+ */
+ @AliasFor(annotation = RestApi.class)
+ String url() default "";
+
+
+ /**
+ * 基本路径,包含host
+ *
+ * @return baseUrl
+ */
+ @AliasFor(annotation = RestApi.class)
+ String baseUrl() default "";
+
+ /**
+ * 是否带上认证token
+ *
+ * @return 结果
+ */
+ @AliasFor(annotation = RestApi.class)
+ boolean credentials() default false;
+}
diff --git a/rest-proxy-api/src/main/java/group/flyfish/rest/annotation/methods/PATCH.java b/rest-proxy-api/src/main/java/group/flyfish/rest/annotation/methods/PATCH.java
new file mode 100644
index 0000000..8f82bd3
--- /dev/null
+++ b/rest-proxy-api/src/main/java/group/flyfish/rest/annotation/methods/PATCH.java
@@ -0,0 +1,67 @@
+package group.flyfish.rest.annotation.methods;
+
+
+import group.flyfish.rest.annotation.RestApi;
+import group.flyfish.rest.enums.HttpMethod;
+import org.springframework.core.annotation.AliasFor;
+
+import java.lang.annotation.*;
+
+/**
+ * 标准的GET声明
+ *
+ * @author wangyu
+ */
+@Target(ElementType.METHOD)
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+@RestApi(method = HttpMethod.PATCH)
+public @interface PATCH {
+
+ /**
+ * uri的别名
+ *
+ * @return 结果
+ */
+ @AliasFor(annotation = RestApi.class)
+ String value() default "";
+
+ /**
+ * 请求uri,使用次标注必须指定BaseUrl或者配置(现在还不支持)
+ *
+ * @return uri
+ */
+ @AliasFor(annotation = RestApi.class)
+ String uri() default "";
+
+ /**
+ * 多个参数时使用合并的body
+ *
+ * @return 结果
+ */
+ boolean mergedBody() default false;
+
+ /**
+ * 可选指定的url,不会从默认地址请求
+ *
+ * @return url
+ */
+ @AliasFor(annotation = RestApi.class)
+ String url() default "";
+
+ /**
+ * 基本路径,包含host
+ *
+ * @return baseUrl
+ */
+ @AliasFor(annotation = RestApi.class)
+ String baseUrl() default "";
+
+ /**
+ * 是否带上认证token
+ *
+ * @return 结果
+ */
+ @AliasFor(annotation = RestApi.class)
+ boolean credentials() default false;
+}
diff --git a/rest-proxy-api/src/main/java/group/flyfish/rest/annotation/methods/POST.java b/rest-proxy-api/src/main/java/group/flyfish/rest/annotation/methods/POST.java
new file mode 100644
index 0000000..6cb3475
--- /dev/null
+++ b/rest-proxy-api/src/main/java/group/flyfish/rest/annotation/methods/POST.java
@@ -0,0 +1,67 @@
+package group.flyfish.rest.annotation.methods;
+
+
+import group.flyfish.rest.annotation.RestApi;
+import group.flyfish.rest.enums.HttpMethod;
+import org.springframework.core.annotation.AliasFor;
+
+import java.lang.annotation.*;
+
+/**
+ * 标准的GET声明
+ *
+ * @author wangyu
+ */
+@Target(ElementType.METHOD)
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+@RestApi(method = HttpMethod.POST)
+public @interface POST {
+
+ /**
+ * uri的别名
+ *
+ * @return 结果
+ */
+ @AliasFor(annotation = RestApi.class)
+ String value() default "";
+
+ /**
+ * 请求uri,使用次标注必须指定BaseUrl或者配置(现在还不支持)
+ *
+ * @return uri
+ */
+ @AliasFor(annotation = RestApi.class)
+ String uri() default "";
+
+ /**
+ * 多个参数时使用合并的body
+ *
+ * @return 结果
+ */
+ boolean mergedBody() default false;
+
+ /**
+ * 可选指定的url,不会从默认地址请求
+ *
+ * @return url
+ */
+ @AliasFor(annotation = RestApi.class)
+ String url() default "";
+
+ /**
+ * 基本路径,包含host
+ *
+ * @return baseUrl
+ */
+ @AliasFor(annotation = RestApi.class)
+ String baseUrl() default "";
+
+ /**
+ * 是否带上认证token
+ *
+ * @return 结果
+ */
+ @AliasFor(annotation = RestApi.class)
+ boolean credentials() default false;
+}
diff --git a/rest-proxy-api/src/main/java/group/flyfish/rest/annotation/methods/PUT.java b/rest-proxy-api/src/main/java/group/flyfish/rest/annotation/methods/PUT.java
new file mode 100644
index 0000000..22b3e7f
--- /dev/null
+++ b/rest-proxy-api/src/main/java/group/flyfish/rest/annotation/methods/PUT.java
@@ -0,0 +1,67 @@
+package group.flyfish.rest.annotation.methods;
+
+
+import group.flyfish.rest.annotation.RestApi;
+import group.flyfish.rest.enums.HttpMethod;
+import org.springframework.core.annotation.AliasFor;
+
+import java.lang.annotation.*;
+
+/**
+ * 标准的GET声明
+ *
+ * @author wangyu
+ */
+@Target(ElementType.METHOD)
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+@RestApi(method = HttpMethod.PUT)
+public @interface PUT {
+
+ /**
+ * uri的别名
+ *
+ * @return 结果
+ */
+ @AliasFor(annotation = RestApi.class)
+ String value() default "";
+
+ /**
+ * 请求uri,使用次标注必须指定BaseUrl或者配置(现在还不支持)
+ *
+ * @return uri
+ */
+ @AliasFor(annotation = RestApi.class)
+ String uri() default "";
+
+ /**
+ * 多个参数时使用合并的body
+ *
+ * @return 结果
+ */
+ boolean mergedBody() default false;
+
+ /**
+ * 可选指定的url,不会从默认地址请求
+ *
+ * @return url
+ */
+ @AliasFor(annotation = RestApi.class)
+ String url() default "";
+
+ /**
+ * 基本路径,包含host
+ *
+ * @return baseUrl
+ */
+ @AliasFor(annotation = RestApi.class)
+ String baseUrl() default "";
+
+ /**
+ * 是否带上认证token
+ *
+ * @return 结果
+ */
+ @AliasFor(annotation = RestApi.class)
+ boolean credentials() default false;
+}
diff --git a/rest-proxy-core/pom.xml b/rest-proxy-core/pom.xml
index 20e8151..c77ff85 100644
--- a/rest-proxy-core/pom.xml
+++ b/rest-proxy-core/pom.xml
@@ -5,7 +5,7 @@
group.flyfish
rest-proxy
- 1.1.1
+ 1.1.2
4.0.0
diff --git a/rest-proxy-core/src/main/java/group/flyfish/rest/constants/RestConstants.java b/rest-proxy-core/src/main/java/group/flyfish/rest/constants/RestConstants.java
index ee9c914..e6f98d2 100644
--- a/rest-proxy-core/src/main/java/group/flyfish/rest/constants/RestConstants.java
+++ b/rest-proxy-core/src/main/java/group/flyfish/rest/constants/RestConstants.java
@@ -218,6 +218,8 @@ public interface RestConstants {
String MSG_THREAD_POOL_EMPTY = "线程池未指定或为空!";
String MSG_IO_ERROR = "发起请求时出现异常!";
String MSG_UNKNOWN_HOST = "未知的请求地址!";
+
+ String MSG_TIME_OUT = "发起请求时服务端响应超时,请检查服务器连接!";
String MSG_REQUEST_ERROR = "请求接口{0}状态异常!代码:{1}";
}
diff --git a/rest-proxy-core/src/main/java/group/flyfish/rest/core/client/DefaultRestClient.java b/rest-proxy-core/src/main/java/group/flyfish/rest/core/client/DefaultRestClient.java
index 58ed0d8..9507df4 100644
--- a/rest-proxy-core/src/main/java/group/flyfish/rest/core/client/DefaultRestClient.java
+++ b/rest-proxy-core/src/main/java/group/flyfish/rest/core/client/DefaultRestClient.java
@@ -13,6 +13,7 @@ import org.apache.http.HttpEntity;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpRequestBase;
+import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.util.EntityUtils;
import org.springframework.http.HttpStatus;
import org.springframework.lang.Nullable;
@@ -282,6 +283,8 @@ final class DefaultRestClient extends RestErrorHandler implements RestClient {
}
} catch (UnknownHostException e) {
handleError(RestConstants.MSG_UNKNOWN_HOST, e);
+ } catch (ConnectTimeoutException e) {
+ handleError(RestConstants.MSG_TIME_OUT, e);
} finally {
request.releaseConnection();
}
diff --git a/rest-proxy-core/src/test/java/group/flyfish/rest/MultipartTest.java b/rest-proxy-core/src/test/java/group/flyfish/rest/MultipartTest.java
index 53074a6..33f82ab 100644
--- a/rest-proxy-core/src/test/java/group/flyfish/rest/MultipartTest.java
+++ b/rest-proxy-core/src/test/java/group/flyfish/rest/MultipartTest.java
@@ -1,11 +1,11 @@
package group.flyfish.rest;
-import group.flyfish.rest.annotation.RestApi;
import group.flyfish.rest.annotation.RestPart;
import group.flyfish.rest.annotation.RestService;
+import group.flyfish.rest.annotation.methods.GET;
+import group.flyfish.rest.annotation.methods.POST;
import group.flyfish.rest.container.RestTestContainer;
import group.flyfish.rest.entity.Multipart;
-import group.flyfish.rest.enums.HttpMethod;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -57,13 +57,13 @@ public class MultipartTest {
@RestService(baseUrl = "http://localhost:8999", timeout = 500)
public interface TestRestService {
- @RestApi(uri = "/files", method = HttpMethod.POST)
+ @POST("/files")
String uploadPart(@RestPart Multipart file, String name, @RestPart("token") Long token);
- @RestApi(uri = "/files", method = HttpMethod.POST)
+ @POST("/files")
String uploadAnno(@RestPart("fbl") InputStream file, @RestPart.Filename("fbl") String name);
- @RestApi(uri = "/files")
+ @GET("/files")
byte[] downloadByte();
}
}
diff --git a/rest-proxy-core/src/test/java/group/flyfish/rest/RestProxyTest.java b/rest-proxy-core/src/test/java/group/flyfish/rest/RestProxyTest.java
index 4e49375..cbaa18d 100644
--- a/rest-proxy-core/src/test/java/group/flyfish/rest/RestProxyTest.java
+++ b/rest-proxy-core/src/test/java/group/flyfish/rest/RestProxyTest.java
@@ -1,9 +1,13 @@
package group.flyfish.rest;
-import group.flyfish.rest.annotation.*;
+import group.flyfish.rest.annotation.AutoMapping;
+import group.flyfish.rest.annotation.RestBody;
+import group.flyfish.rest.annotation.RestPathParam;
+import group.flyfish.rest.annotation.RestService;
+import group.flyfish.rest.annotation.methods.GET;
+import group.flyfish.rest.annotation.methods.POST;
import group.flyfish.rest.container.RestTestContainer;
import group.flyfish.rest.domain.TestItem;
-import group.flyfish.rest.enums.HttpMethod;
import group.flyfish.rest.mapping.TestRestResultMapping;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
@@ -56,7 +60,7 @@ public class RestProxyTest {
@AutoMapping(TestRestResultMapping.class)
public interface TestRestService {
- @RestApi(uri = "/video/{platformId}/cameras/all")
+ @GET("/video/{platformId}/cameras/all")
List getCameras(@RestPathParam String platformId, String regionCode, String name1);
}
@@ -65,7 +69,7 @@ public class RestProxyTest {
@AutoMapping(TestRestResultMapping.class)
public interface TestPostService {
- @RestApi(method = HttpMethod.POST, uri = "/getResources")
+ @POST("/getResources")
Map>> getResources(@RestBody Map body);
}
}
diff --git a/rest-proxy-core/src/test/java/group/flyfish/rest/SimpleProxyTest.java b/rest-proxy-core/src/test/java/group/flyfish/rest/SimpleProxyTest.java
index f189de8..18cffdf 100644
--- a/rest-proxy-core/src/test/java/group/flyfish/rest/SimpleProxyTest.java
+++ b/rest-proxy-core/src/test/java/group/flyfish/rest/SimpleProxyTest.java
@@ -1,6 +1,7 @@
package group.flyfish.rest;
import group.flyfish.rest.annotation.*;
+import group.flyfish.rest.annotation.methods.GET;
import group.flyfish.rest.container.RestTestContainer;
import group.flyfish.rest.domain.TestResult;
import group.flyfish.rest.mapping.TestRestResultMapping;
@@ -36,7 +37,7 @@ public class SimpleProxyTest {
values.put("b", 2);
TestResult query = new TestResult<>();
query.setMsg("asdasd");
- Map result = testSimpleService.getDirectories("123456", "admin", values, query);
+ Map result = testSimpleService.getDirectories("123456", "admin", values, query);
if (null != result) {
log.info(result.toString());
}
@@ -52,7 +53,7 @@ public class SimpleProxyTest {
* @param token token
* @return 结果
*/
- @RestApi("/financial/public/income")
+ @GET("/financial/public/income")
Map getDirectories(@RestHeader("Authorization") String token, @RestParam("userId") String userId,
Map others, @RestParams TestResult result);
}