This is an automated email from the ASF dual-hosted git repository.
zhangzicheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new 9363bfcd3 [ISSUE #3485]refactor client: simplify the
ShenyuSpringMvcClient with AliasFor annotation. (#3530)
9363bfcd3 is described below
commit 9363bfcd328a3673e55892d59ba19e91f5b1b9e9
Author: Zihao Huang <[email protected]>
AuthorDate: Sat Jun 11 18:01:27 2022 +0800
[ISSUE #3485]refactor client: simplify the ShenyuSpringMvcClient with
AliasFor annotation. (#3530)
---
.../springmvc/annotation/ShenyuSpringMvcClient.java | 12 +++++++++++-
.../init/SpringMvcClientBeanPostProcessor.java | 17 +++++++++++------
.../examples/http/controller/HttpTestController.java | 2 +-
.../examples/http/controller/OrderController.java | 12 ++++++------
.../examples/http/controller/RequestController.java | 2 +-
.../http/controller/ShenyuClientPathController.java | 6 +++---
.../examples/https/controller/OrderController.java | 4 ++--
.../springmvc/controller/HttpTestController.java | 2 +-
.../examples/springmvc/controller/OrderController.java | 12 ++++++------
.../springmvc/controller/HttpTestController.java | 2 +-
.../examples/springmvc/controller/OrderController.java | 12 ++++++------
.../websocket/controller/TestHttpController.java | 2 +-
12 files changed, 50 insertions(+), 35 deletions(-)
diff --git
a/shenyu-client/shenyu-client-http/shenyu-client-springmvc/src/main/java/org/apache/shenyu/client/springmvc/annotation/ShenyuSpringMvcClient.java
b/shenyu-client/shenyu-client-http/shenyu-client-springmvc/src/main/java/org/apache/shenyu/client/springmvc/annotation/ShenyuSpringMvcClient.java
index a72d6021c..036bea463 100644
---
a/shenyu-client/shenyu-client-http/shenyu-client-springmvc/src/main/java/org/apache/shenyu/client/springmvc/annotation/ShenyuSpringMvcClient.java
+++
b/shenyu-client/shenyu-client-http/shenyu-client-springmvc/src/main/java/org/apache/shenyu/client/springmvc/annotation/ShenyuSpringMvcClient.java
@@ -17,6 +17,8 @@
package org.apache.shenyu.client.springmvc.annotation;
+import org.springframework.core.annotation.AliasFor;
+
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@@ -34,8 +36,16 @@ public @interface ShenyuSpringMvcClient {
*
* @return the string
*/
+ @AliasFor(attribute = "path")
+ String value() default "";
+
+ /**
+ * Path string.
+ *
+ * @return the string
+ */
+ @AliasFor(attribute = "value")
String path() default "";
-
/**
* Rule name string.
*
diff --git
a/shenyu-client/shenyu-client-http/shenyu-client-springmvc/src/main/java/org/apache/shenyu/client/springmvc/init/SpringMvcClientBeanPostProcessor.java
b/shenyu-client/shenyu-client-http/shenyu-client-springmvc/src/main/java/org/apache/shenyu/client/springmvc/init/SpringMvcClientBeanPostProcessor.java
index b9e9a005c..ebe51f231 100644
---
a/shenyu-client/shenyu-client-http/shenyu-client-springmvc/src/main/java/org/apache/shenyu/client/springmvc/init/SpringMvcClientBeanPostProcessor.java
+++
b/shenyu-client/shenyu-client-http/shenyu-client-springmvc/src/main/java/org/apache/shenyu/client/springmvc/init/SpringMvcClientBeanPostProcessor.java
@@ -30,6 +30,7 @@ import
org.apache.shenyu.register.common.config.PropertiesConfig;
import org.apache.shenyu.register.common.dto.MetaDataRegisterDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.annotation.AnnotatedElementUtils;
@@ -104,22 +105,26 @@ public class SpringMvcClientBeanPostProcessor implements
BeanPostProcessor {
@Override
public Object postProcessAfterInitialization(@NonNull final Object bean,
@NonNull final String beanName) throws BeansException {
+ Class<?> clazz = bean.getClass();
+ if (AopUtils.isAopProxy(bean)) {
+ clazz = AopUtils.getTargetClass(bean);
+ }
// Filter out is not controller out
if (Boolean.TRUE.equals(isFull) || !hasAnnotation(bean.getClass(),
Controller.class)) {
return bean;
}
- final ShenyuSpringMvcClient beanShenyuClient =
AnnotationUtils.findAnnotation(bean.getClass(), ShenyuSpringMvcClient.class);
- final String superPath = buildApiSuperPath(bean.getClass());
+ final ShenyuSpringMvcClient beanShenyuClient =
AnnotatedElementUtils.findMergedAnnotation(clazz, ShenyuSpringMvcClient.class);
+ final String superPath = buildApiSuperPath(clazz);
// Compatible with previous versions
if (Objects.nonNull(beanShenyuClient) && superPath.contains("*")) {
publisher.publishEvent(buildMetaDataDTO(beanShenyuClient,
pathJoin(contextPath, superPath)));
return bean;
}
- final Method[] methods =
ReflectionUtils.getUniqueDeclaredMethods(bean.getClass());
+ final Method[] methods =
ReflectionUtils.getUniqueDeclaredMethods(clazz);
for (Method method : methods) {
final RequestMapping requestMapping =
AnnotatedElementUtils.findMergedAnnotation(method, RequestMapping.class);
- ShenyuSpringMvcClient methodShenyuClient =
AnnotationUtils.findAnnotation(method, ShenyuSpringMvcClient.class);
+ ShenyuSpringMvcClient methodShenyuClient =
AnnotatedElementUtils.findMergedAnnotation(method, ShenyuSpringMvcClient.class);
methodShenyuClient = Objects.isNull(methodShenyuClient) ?
beanShenyuClient : methodShenyuClient;
// the result of ReflectionUtils#getUniqueDeclaredMethods contains
method such as hashCode, wait, toSting
// add Objects.nonNull(requestMapping) to make sure not register
wrong method
@@ -136,7 +141,7 @@ public class SpringMvcClientBeanPostProcessor implements
BeanPostProcessor {
}
private String buildApiPath(@NonNull final Method method, @NonNull final
String superPath) {
- ShenyuSpringMvcClient shenyuSpringMvcClient =
AnnotationUtils.findAnnotation(method, ShenyuSpringMvcClient.class);
+ ShenyuSpringMvcClient shenyuSpringMvcClient =
AnnotatedElementUtils.findMergedAnnotation(method, ShenyuSpringMvcClient.class);
if (Objects.nonNull(shenyuSpringMvcClient) &&
StringUtils.isNotBlank(shenyuSpringMvcClient.path())) {
return pathJoin(contextPath, superPath,
shenyuSpringMvcClient.path());
}
@@ -175,7 +180,7 @@ public class SpringMvcClientBeanPostProcessor implements
BeanPostProcessor {
}
private String buildApiSuperPath(@NonNull final Class<?> method) {
- ShenyuSpringMvcClient shenyuSpringMvcClient =
AnnotationUtils.findAnnotation(method, ShenyuSpringMvcClient.class);
+ ShenyuSpringMvcClient shenyuSpringMvcClient =
AnnotatedElementUtils.findMergedAnnotation(method, ShenyuSpringMvcClient.class);
if (Objects.nonNull(shenyuSpringMvcClient) &&
StringUtils.isNotBlank(shenyuSpringMvcClient.path())) {
return shenyuSpringMvcClient.path();
}
diff --git
a/shenyu-examples/shenyu-examples-http/src/main/java/org/apache/shenyu/examples/http/controller/HttpTestController.java
b/shenyu-examples/shenyu-examples-http/src/main/java/org/apache/shenyu/examples/http/controller/HttpTestController.java
index 3ab9c1ac1..cc5bc6f0a 100644
---
a/shenyu-examples/shenyu-examples-http/src/main/java/org/apache/shenyu/examples/http/controller/HttpTestController.java
+++
b/shenyu-examples/shenyu-examples-http/src/main/java/org/apache/shenyu/examples/http/controller/HttpTestController.java
@@ -60,7 +60,7 @@ import java.util.Map;
*/
@RestController
@RequestMapping("/test")
-@ShenyuSpringMvcClient(path = "/test/**")
+@ShenyuSpringMvcClient("/test/**")
public class HttpTestController {
private static final Logger LOGGER =
LoggerFactory.getLogger(HttpTestController.class);
diff --git
a/shenyu-examples/shenyu-examples-http/src/main/java/org/apache/shenyu/examples/http/controller/OrderController.java
b/shenyu-examples/shenyu-examples-http/src/main/java/org/apache/shenyu/examples/http/controller/OrderController.java
index 2e9733c0d..ac9f8bc71 100644
---
a/shenyu-examples/shenyu-examples-http/src/main/java/org/apache/shenyu/examples/http/controller/OrderController.java
+++
b/shenyu-examples/shenyu-examples-http/src/main/java/org/apache/shenyu/examples/http/controller/OrderController.java
@@ -38,7 +38,7 @@ import java.util.Objects;
*/
@RestController
@RequestMapping("/order")
-@ShenyuSpringMvcClient(path = "/order")
+@ShenyuSpringMvcClient("/order")
public class OrderController {
/**
@@ -48,7 +48,7 @@ public class OrderController {
* @return the order dto
*/
@PostMapping("/save")
- @ShenyuSpringMvcClient(path = "/save", desc = "Save order")
+ @ShenyuSpringMvcClient("/save")
public OrderDTO save(@RequestBody final OrderDTO orderDTO) {
orderDTO.setName("hello world save order");
return orderDTO;
@@ -61,7 +61,7 @@ public class OrderController {
* @return the order dto
*/
@GetMapping("/findById")
- @ShenyuSpringMvcClient(path = "/findById", desc = "Find by id")
+ @ShenyuSpringMvcClient("/findById")
public OrderDTO findById(@RequestParam("id") final String id) {
return build(id, "hello world findById");
}
@@ -74,7 +74,7 @@ public class OrderController {
* @return the path variable
*/
@GetMapping("/path/{id}/{name}")
- @ShenyuSpringMvcClient(path = "/path/**")
+ @ShenyuSpringMvcClient("/path/**")
public OrderDTO getPathVariable(@PathVariable("id") final String id,
@PathVariable("name") final String name) {
return build(id, "hello world restful: " + name);
}
@@ -86,13 +86,13 @@ public class OrderController {
* @return the order dto
*/
@GetMapping("/path/{id}/name")
- @ShenyuSpringMvcClient(path = "/path/**/name")
+ @ShenyuSpringMvcClient("/path/**/name")
public OrderDTO testRestFul(@PathVariable("id") final String id) {
return build(id, "hello world restful inline " + id);
}
@GetMapping("/oauth2/test")
- @ShenyuSpringMvcClient(path = "/oauth2/test")
+ @ShenyuSpringMvcClient("/oauth2/test")
public OAuth2DTO testRestFul(final ServerHttpRequest request) {
HttpHeaders headers = request.getHeaders();
List<String> tokens = headers.get("Authorization");
diff --git
a/shenyu-examples/shenyu-examples-http/src/main/java/org/apache/shenyu/examples/http/controller/RequestController.java
b/shenyu-examples/shenyu-examples-http/src/main/java/org/apache/shenyu/examples/http/controller/RequestController.java
index ef8664470..274ccfde8 100644
---
a/shenyu-examples/shenyu-examples-http/src/main/java/org/apache/shenyu/examples/http/controller/RequestController.java
+++
b/shenyu-examples/shenyu-examples-http/src/main/java/org/apache/shenyu/examples/http/controller/RequestController.java
@@ -35,7 +35,7 @@ import reactor.core.publisher.Mono;
*/
@RestController
@RequestMapping("/request")
-@ShenyuSpringMvcClient(path = "/request/**")
+@ShenyuSpringMvcClient("/request/**")
public class RequestController {
private static final Logger LOGGER =
LoggerFactory.getLogger(RequestController.class);
diff --git
a/shenyu-examples/shenyu-examples-http/src/main/java/org/apache/shenyu/examples/http/controller/ShenyuClientPathController.java
b/shenyu-examples/shenyu-examples-http/src/main/java/org/apache/shenyu/examples/http/controller/ShenyuClientPathController.java
index 221ceedfe..e69da14d6 100644
---
a/shenyu-examples/shenyu-examples-http/src/main/java/org/apache/shenyu/examples/http/controller/ShenyuClientPathController.java
+++
b/shenyu-examples/shenyu-examples-http/src/main/java/org/apache/shenyu/examples/http/controller/ShenyuClientPathController.java
@@ -36,7 +36,7 @@ public class ShenyuClientPathController {
* @return result
*/
@RequestMapping("shenyu/client/hello")
- @ShenyuSpringMvcClient(path = "shenyu/client/hello", desc = "shenyu client
annotation register")
+ @ShenyuSpringMvcClient("shenyu/client/hello")
public String hello() {
return "hello! " + HELLO_SUFFIX;
}
@@ -47,7 +47,7 @@ public class ShenyuClientPathController {
* @return result
*/
@RequestMapping("shenyu/client/timeout")
- @ShenyuSpringMvcClient(path = "shenyu/client/timeout", desc = "shenyu
client annotation register")
+ @ShenyuSpringMvcClient("shenyu/client/timeout")
public String timeout() {
System.out.println("timeout");
try {
@@ -65,7 +65,7 @@ public class ShenyuClientPathController {
* @return result
*/
@RequestMapping("shenyu/client/hi")
- @ShenyuSpringMvcClient(path = "shenyu/client/hi", desc = "shenyu client
annotation register")
+ @ShenyuSpringMvcClient("shenyu/client/hi")
public String hello(final String name) {
return "hi! " + name + "! " + HELLO_SUFFIX;
}
diff --git
a/shenyu-examples/shenyu-examples-https/src/main/java/org/apache/shenyu/examples/https/controller/OrderController.java
b/shenyu-examples/shenyu-examples-https/src/main/java/org/apache/shenyu/examples/https/controller/OrderController.java
index ef5004225..f5151ff37 100644
---
a/shenyu-examples/shenyu-examples-https/src/main/java/org/apache/shenyu/examples/https/controller/OrderController.java
+++
b/shenyu-examples/shenyu-examples-https/src/main/java/org/apache/shenyu/examples/https/controller/OrderController.java
@@ -29,7 +29,7 @@ import org.springframework.web.bind.annotation.RestController;
*/
@RestController
@RequestMapping("/order")
-@ShenyuSpringMvcClient(path = "/order")
+@ShenyuSpringMvcClient("/order")
public class OrderController {
/**
@@ -39,7 +39,7 @@ public class OrderController {
* @return the order dto
*/
@GetMapping("/findById")
- @ShenyuSpringMvcClient(path = "/findById", desc = "Find by id")
+ @ShenyuSpringMvcClient("/findById")
public OrderDTO findById(@RequestParam("id") final String id) {
OrderDTO dto = new OrderDTO();
dto.setId(id);
diff --git
a/shenyu-examples/shenyu-examples-springmvc-tomcat/src/main/java/org/apache/shenyu/examples/springmvc/controller/HttpTestController.java
b/shenyu-examples/shenyu-examples-springmvc-tomcat/src/main/java/org/apache/shenyu/examples/springmvc/controller/HttpTestController.java
index d601a8cfa..dc13ce0bc 100644
---
a/shenyu-examples/shenyu-examples-springmvc-tomcat/src/main/java/org/apache/shenyu/examples/springmvc/controller/HttpTestController.java
+++
b/shenyu-examples/shenyu-examples-springmvc-tomcat/src/main/java/org/apache/shenyu/examples/springmvc/controller/HttpTestController.java
@@ -38,7 +38,7 @@ import org.springframework.web.bind.annotation.RestController;
*/
@RestController
@RequestMapping("/test")
-@ShenyuSpringMvcClient(path = "/test/**")
+@ShenyuSpringMvcClient("/test/**")
public class HttpTestController {
/**
diff --git
a/shenyu-examples/shenyu-examples-springmvc-tomcat/src/main/java/org/apache/shenyu/examples/springmvc/controller/OrderController.java
b/shenyu-examples/shenyu-examples-springmvc-tomcat/src/main/java/org/apache/shenyu/examples/springmvc/controller/OrderController.java
index 922a9fb13..354d74cee 100644
---
a/shenyu-examples/shenyu-examples-springmvc-tomcat/src/main/java/org/apache/shenyu/examples/springmvc/controller/OrderController.java
+++
b/shenyu-examples/shenyu-examples-springmvc-tomcat/src/main/java/org/apache/shenyu/examples/springmvc/controller/OrderController.java
@@ -35,7 +35,7 @@ import org.springframework.web.bind.annotation.RestController;
*/
@RestController
@RequestMapping("/order")
-@ShenyuSpringMvcClient(path = "/order")
+@ShenyuSpringMvcClient("/order")
public class OrderController {
/**
@@ -45,7 +45,7 @@ public class OrderController {
* @return the order dto
*/
@PostMapping("/save")
- @ShenyuSpringMvcClient(path = "/save", desc = "Save order")
+ @ShenyuSpringMvcClient("/save")
public OrderDTO save(@RequestBody final OrderDTO orderDTO) {
orderDTO.setName("hello world save order");
return orderDTO;
@@ -58,7 +58,7 @@ public class OrderController {
* @return the order dto
*/
@GetMapping("/findById")
- @ShenyuSpringMvcClient(path = "/findById", desc = "Find by id")
+ @ShenyuSpringMvcClient("/findById")
public OrderDTO findById(@RequestParam("id") final String id) {
return build(id,"hello world findById");
}
@@ -71,7 +71,7 @@ public class OrderController {
* @return the path variable
*/
@GetMapping("/path/{id}/{name}")
- @ShenyuSpringMvcClient(path = "/path/**")
+ @ShenyuSpringMvcClient("/path/**")
public OrderDTO getPathVariable(@PathVariable("id") final String id,
@PathVariable("name") final String name) {
return build(id,"hello world restful: " + name);
}
@@ -83,13 +83,13 @@ public class OrderController {
* @return the order dto
*/
@GetMapping("/path/{id}/name")
- @ShenyuSpringMvcClient(path = "/path/**/name")
+ @ShenyuSpringMvcClient("/path/**/name")
public OrderDTO testRestFul(@PathVariable("id") final String id) {
return build(id,"hello world restful inline " + id);
}
@GetMapping("/oauth2/test")
- @ShenyuSpringMvcClient(path = "/oauth2/test")
+ @ShenyuSpringMvcClient("/oauth2/test")
public OAuth2DTO testRestFul(final HttpServletRequest request) {
final String token = request.getHeader("Authorization");
final OAuth2DTO oAuth2DTO = new OAuth2DTO();
diff --git
a/shenyu-examples/shenyu-examples-springmvc/src/main/java/org/apache/shenyu/examples/springmvc/controller/HttpTestController.java
b/shenyu-examples/shenyu-examples-springmvc/src/main/java/org/apache/shenyu/examples/springmvc/controller/HttpTestController.java
index b77cbe225..b6bb4449f 100644
---
a/shenyu-examples/shenyu-examples-springmvc/src/main/java/org/apache/shenyu/examples/springmvc/controller/HttpTestController.java
+++
b/shenyu-examples/shenyu-examples-springmvc/src/main/java/org/apache/shenyu/examples/springmvc/controller/HttpTestController.java
@@ -38,7 +38,7 @@ import org.springframework.web.bind.annotation.RestController;
*/
@RestController
@RequestMapping("/test")
-@ShenyuSpringMvcClient(path = "/test/**")
+@ShenyuSpringMvcClient("/test/**")
public class HttpTestController {
/**
diff --git
a/shenyu-examples/shenyu-examples-springmvc/src/main/java/org/apache/shenyu/examples/springmvc/controller/OrderController.java
b/shenyu-examples/shenyu-examples-springmvc/src/main/java/org/apache/shenyu/examples/springmvc/controller/OrderController.java
index 922a9fb13..4fc9ecbfb 100644
---
a/shenyu-examples/shenyu-examples-springmvc/src/main/java/org/apache/shenyu/examples/springmvc/controller/OrderController.java
+++
b/shenyu-examples/shenyu-examples-springmvc/src/main/java/org/apache/shenyu/examples/springmvc/controller/OrderController.java
@@ -35,7 +35,7 @@ import org.springframework.web.bind.annotation.RestController;
*/
@RestController
@RequestMapping("/order")
-@ShenyuSpringMvcClient(path = "/order")
+@ShenyuSpringMvcClient( "/order")
public class OrderController {
/**
@@ -45,7 +45,7 @@ public class OrderController {
* @return the order dto
*/
@PostMapping("/save")
- @ShenyuSpringMvcClient(path = "/save", desc = "Save order")
+ @ShenyuSpringMvcClient("/save")
public OrderDTO save(@RequestBody final OrderDTO orderDTO) {
orderDTO.setName("hello world save order");
return orderDTO;
@@ -58,7 +58,7 @@ public class OrderController {
* @return the order dto
*/
@GetMapping("/findById")
- @ShenyuSpringMvcClient(path = "/findById", desc = "Find by id")
+ @ShenyuSpringMvcClient("/findById")
public OrderDTO findById(@RequestParam("id") final String id) {
return build(id,"hello world findById");
}
@@ -71,7 +71,7 @@ public class OrderController {
* @return the path variable
*/
@GetMapping("/path/{id}/{name}")
- @ShenyuSpringMvcClient(path = "/path/**")
+ @ShenyuSpringMvcClient("/path/**")
public OrderDTO getPathVariable(@PathVariable("id") final String id,
@PathVariable("name") final String name) {
return build(id,"hello world restful: " + name);
}
@@ -83,13 +83,13 @@ public class OrderController {
* @return the order dto
*/
@GetMapping("/path/{id}/name")
- @ShenyuSpringMvcClient(path = "/path/**/name")
+ @ShenyuSpringMvcClient("/path/**/name")
public OrderDTO testRestFul(@PathVariable("id") final String id) {
return build(id,"hello world restful inline " + id);
}
@GetMapping("/oauth2/test")
- @ShenyuSpringMvcClient(path = "/oauth2/test")
+ @ShenyuSpringMvcClient("/oauth2/test")
public OAuth2DTO testRestFul(final HttpServletRequest request) {
final String token = request.getHeader("Authorization");
final OAuth2DTO oAuth2DTO = new OAuth2DTO();
diff --git
a/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-native-websocket/src/main/java/org/apache/shenyu/examples/websocket/controller/TestHttpController.java
b/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-native-websocket/src/main/java/org/apache/shenyu/examples/websocket/controller/TestHttpController.java
index 8a2c121e2..4163fd998 100644
---
a/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-native-websocket/src/main/java/org/apache/shenyu/examples/websocket/controller/TestHttpController.java
+++
b/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-native-websocket/src/main/java/org/apache/shenyu/examples/websocket/controller/TestHttpController.java
@@ -32,7 +32,7 @@ import java.io.IOException;
*/
@Controller
@RequestMapping("/ws")
-@ShenyuSpringMvcClient(path = "/ws/**")
+@ShenyuSpringMvcClient("/ws/**")
public class TestHttpController {
@RequestMapping("/sendMsg")