Feature/dubbo3.2 add rest method annotation support judge (#12835)

* support reExport

* add rest method annotation support judge

---------

Co-authored-by: suncr <suncairong@moresec.cn>
This commit is contained in:
suncairong163 2023-08-03 13:05:09 +08:00 committed by GitHub
parent 521b3e0146
commit 1e18b359e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 85 additions and 2 deletions

View File

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

View File

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

View File

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

View File

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