feat: 升级1.1.2,增加标准注解

This commit is contained in:
wangyu 2023-04-13 09:30:50 +08:00
parent 3f9755df0b
commit 6e8966246e
14 changed files with 355 additions and 23 deletions

View File

@ -13,7 +13,7 @@
<groupId>group.flyfish</groupId>
<artifactId>rest-proxy</artifactId>
<version>1.1.1</version>
<version>1.1.2</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
@ -22,7 +22,7 @@
<java.version>1.8</java.version>
<commons-collection.version>4.4</commons-collection.version>
<commons.lang.version>2.6</commons.lang.version>
<sdk.version>1.1.1</sdk.version>
<sdk.version>1.1.2</sdk.version>
</properties>
<packaging>pom</packaging>

View File

@ -5,7 +5,7 @@
<parent>
<groupId>group.flyfish</groupId>
<artifactId>rest-proxy</artifactId>
<version>1.1.1</version>
<version>1.1.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -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
*

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -5,7 +5,7 @@
<parent>
<groupId>group.flyfish</groupId>
<artifactId>rest-proxy</artifactId>
<version>1.1.1</version>
<version>1.1.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -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}";
}

View File

@ -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();
}

View File

@ -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();
}
}

View File

@ -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<TestItem> 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<String, List<Map<String, Object>>> getResources(@RestBody Map<String, Object> body);
}
}

View File

@ -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;
@ -52,7 +53,7 @@ public class SimpleProxyTest {
* @param token token
* @return 结果
*/
@RestApi("/financial/public/income")
@GET("/financial/public/income")
Map<String, Object> getDirectories(@RestHeader("Authorization") String token, @RestParam("userId") String userId,
Map<String, Object> others, @RestParams TestResult<Void> result);
}