This is an automated email from the ASF dual-hosted git repository.

crazyhzm pushed a commit to branch 3.2
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.2 by this push:
     new 1e18b359e2 Feature/dubbo3.2 add rest method annotation support judge 
(#12835)
1e18b359e2 is described below

commit 1e18b359e22b25a27a871c5e004281da9cd1484e
Author: suncairong163 <[email protected]>
AuthorDate: Thu Aug 3 13:05:09 2023 +0800

    Feature/dubbo3.2 add rest method annotation support judge (#12835)
    
    * support reExport
    
    * add rest method annotation support judge
    
    ---------
    
    Co-authored-by: suncr <[email protected]>
---
 .../rest/AbstractServiceRestMetadataResolver.java  | 13 +++++
 .../jaxrs/JAXRSServiceRestMetadataResolver.java    |  4 +-
 .../SpringMvcServiceRestMetadataResolver.java      |  6 +-
 .../rest/NoAnnotationApiDemoResolverTest.java      | 64 ++++++++++++++++++++++
 4 files changed, 85 insertions(+), 2 deletions(-)

diff --git 
a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/AbstractServiceRestMetadataResolver.java
 
b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/AbstractServiceRestMetadataResolver.java
index 615911e93c..ed00284b17 100644
--- 
a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/AbstractServiceRestMetadataResolver.java
+++ 
b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/AbstractServiceRestMetadataResolver.java
@@ -40,6 +40,7 @@ import static java.util.Collections.emptyList;
 import static java.util.Collections.sort;
 import static java.util.Collections.unmodifiableMap;
 import static org.apache.dubbo.common.function.ThrowableFunction.execute;
+import static 
org.apache.dubbo.common.utils.AnnotationUtils.isAnnotationPresent;
 import static 
org.apache.dubbo.common.utils.AnnotationUtils.isAnyAnnotationPresent;
 import static org.apache.dubbo.common.utils.ClassUtils.forName;
 import static org.apache.dubbo.common.utils.ClassUtils.getAllInterfaces;
@@ -421,4 +422,16 @@ public abstract class AbstractServiceRestMetadataResolver 
implements ServiceRest
 
         return supportedExtensionInstances;
     }
+
+    public static boolean isServiceMethodAnnotationPresent(Class<?> 
serviceImpl, String annotationClass) {
+        List<Method> allMethods = getAllMethods(serviceImpl, 
excludedDeclaredClass(Object.class));
+
+        for (Method method : allMethods) {
+            if (isAnnotationPresent(method, annotationClass)) {
+                return true;
+            }
+        }
+
+        return false;
+    }
 }
diff --git 
a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/jaxrs/JAXRSServiceRestMetadataResolver.java
 
b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/jaxrs/JAXRSServiceRestMetadataResolver.java
index 1e107edc22..40341672b1 100644
--- 
a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/jaxrs/JAXRSServiceRestMetadataResolver.java
+++ 
b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/jaxrs/JAXRSServiceRestMetadataResolver.java
@@ -47,7 +47,9 @@ public class JAXRSServiceRestMetadataResolver extends 
AbstractServiceRestMetadat
 
     @Override
     protected boolean supports0(Class<?> serviceType) {
-        return isAnnotationPresent(serviceType, PATH_ANNOTATION_CLASS_NAME);
+        return isAnnotationPresent(serviceType, PATH_ANNOTATION_CLASS_NAME)
+            // method @Path
+            || 
isServiceMethodAnnotationPresent(serviceType,PATH_ANNOTATION_CLASS_NAME);
     }
 
     @Override
diff --git 
a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/springmvc/SpringMvcServiceRestMetadataResolver.java
 
b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/springmvc/SpringMvcServiceRestMetadataResolver.java
index f7e89540c4..f0b1681e5a 100644
--- 
a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/springmvc/SpringMvcServiceRestMetadataResolver.java
+++ 
b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/springmvc/SpringMvcServiceRestMetadataResolver.java
@@ -39,6 +39,7 @@ import static 
org.apache.dubbo.common.utils.PathUtils.buildPath;
 import static 
org.apache.dubbo.metadata.rest.RestMetadataConstants.SPRING_MVC.ANNOTATED_ELEMENT_UTILS_CLASS;
 import static 
org.apache.dubbo.metadata.rest.RestMetadataConstants.SPRING_MVC.CONTROLLER_ANNOTATION_CLASS;
 import static 
org.apache.dubbo.metadata.rest.RestMetadataConstants.SPRING_MVC.REQUEST_MAPPING_ANNOTATION_CLASS;
+import static 
org.apache.dubbo.metadata.rest.RestMetadataConstants.SPRING_MVC.REQUEST_MAPPING_ANNOTATION_CLASS_NAME;
 
 /**
  * {@link ServiceRestMetadataResolver}
@@ -56,7 +57,10 @@ public class SpringMvcServiceRestMetadataResolver extends 
AbstractServiceRestMet
     @Override
     protected boolean supports0(Class<?> serviceType) {
         // class @Controller or @RequestMapping
-        return isAnnotationPresent(serviceType, CONTROLLER_ANNOTATION_CLASS) 
|| isAnnotationPresent(serviceType, REQUEST_MAPPING_ANNOTATION_CLASS);
+        return isAnnotationPresent(serviceType, CONTROLLER_ANNOTATION_CLASS)
+            || isAnnotationPresent(serviceType, 
REQUEST_MAPPING_ANNOTATION_CLASS)
+            // method @RequestMapping
+            || isServiceMethodAnnotationPresent(serviceType, 
REQUEST_MAPPING_ANNOTATION_CLASS_NAME);
     }
 
     @Override
diff --git 
a/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/rest/NoAnnotationApiDemoResolverTest.java
 
b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/rest/NoAnnotationApiDemoResolverTest.java
new file mode 100644
index 0000000000..ad51ea2740
--- /dev/null
+++ 
b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/rest/NoAnnotationApiDemoResolverTest.java
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.dubbo.metadata.rest;
+
+import org.apache.dubbo.metadata.rest.jaxrs.JAXRSServiceRestMetadataResolver;
+import 
org.apache.dubbo.metadata.rest.springmvc.SpringMvcServiceRestMetadataResolver;
+import org.apache.dubbo.rpc.model.ApplicationModel;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.QueryParam;
+
+public class NoAnnotationApiDemoResolverTest {
+    private JAXRSServiceRestMetadataResolver jaxrsServiceRestMetadataResolver 
= new JAXRSServiceRestMetadataResolver(ApplicationModel.defaultModel());
+    private SpringMvcServiceRestMetadataResolver 
springMvcServiceRestMetadataResolver = new 
SpringMvcServiceRestMetadataResolver(ApplicationModel.defaultModel());
+
+    @Test
+    void testNoAnnotationApiResolver() {
+        
Assertions.assertTrue(jaxrsServiceRestMetadataResolver.supports(JaxrsNoAnnotationApiDemoImpl.class));
+        
Assertions.assertTrue(springMvcServiceRestMetadataResolver.supports(SpringMvcNoAnnotationApiDemoImpl.class));
+    }
+}
+
+class JaxrsNoAnnotationApiDemoImpl implements NoAnnotationApiDemo {
+    @Override
+    @Path("/test")
+    @GET
+    public String test(@QueryParam("test") String test) {
+        return "success" + test;
+    }
+}
+
+class SpringMvcNoAnnotationApiDemoImpl implements NoAnnotationApiDemo {
+    @Override
+    @RequestMapping("/test")
+    public String test(@RequestBody() String test) {
+        return "success" + test;
+    }
+}
+
+interface NoAnnotationApiDemo {
+
+    String test(String test);
+}
+
+

Reply via email to