diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/ApiMappingHandlerMapping.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/ApiMappingHandlerMapping.java index 074cead3..17f4b765 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/ApiMappingHandlerMapping.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/ApiMappingHandlerMapping.java @@ -4,8 +4,8 @@ import com.gitee.sop.servercommon.annotation.ApiAbility; import com.gitee.sop.servercommon.annotation.ApiMapping; import com.gitee.sop.servercommon.bean.ServiceConfig; import com.gitee.sop.servercommon.bean.ServiceContext; +import com.gitee.sop.servercommon.util.OpenUtil; import org.springframework.core.PriorityOrdered; -import org.springframework.core.annotation.AnnotationUtils; import org.springframework.util.StringValueResolver; import org.springframework.web.servlet.mvc.condition.RequestCondition; import org.springframework.web.servlet.mvc.method.RequestMappingInfo; @@ -26,7 +26,7 @@ public class ApiMappingHandlerMapping extends RequestMappingHandlerMapping imple String sopMvc = System.getProperty(ServiceContext.SOP_MVC); boolean isMvc = sopMvc != null; ApiMapping apiMapping = method.getAnnotation(ApiMapping.class); - ApiAbility apiAbility = method.getAnnotation(ApiAbility.class); + ApiAbility apiAbility = OpenUtil.getAnnotationFromMethodOrClass(method, ApiAbility.class); StringValueResolver valueResolver = null; if (apiMapping != null || apiAbility != null) { valueResolver = isMvc ? stringValueResolverMVC : stringValueResolver; @@ -53,7 +53,7 @@ public class ApiMappingHandlerMapping extends RequestMappingHandlerMapping imple permission = apiMapping.permission(); needToken = apiMapping.needToken(); } else { - ApiAbility apiAbility = this.findApiAbilityAnnotation(method); + ApiAbility apiAbility = OpenUtil.getAnnotationFromMethodOrClass(method, ApiAbility.class); if (apiAbility != null) { version = apiAbility.version(); ignoreValidate = apiAbility.ignoreValidate(); @@ -88,12 +88,4 @@ public class ApiMappingHandlerMapping extends RequestMappingHandlerMapping imple return new ApiMappingRequestCondition(apiMappingInfo); } - protected ApiAbility findApiAbilityAnnotation(Method method) { - ApiAbility apiAbility = method.getAnnotation(ApiAbility.class); - if (apiAbility == null) { - Class controllerClass = method.getDeclaringClass(); - apiAbility = AnnotationUtils.findAnnotation(controllerClass, ApiAbility.class); - } - return apiAbility; - } } \ No newline at end of file diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/param/ApiArgumentResolver.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/param/ApiArgumentResolver.java index 82c3be81..d099b0b5 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/param/ApiArgumentResolver.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/param/ApiArgumentResolver.java @@ -21,7 +21,6 @@ import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.multipart.MultipartRequest; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; -import org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor; import org.springframework.web.servlet.mvc.method.annotation.ServletRequestMethodArgumentResolver; import javax.servlet.ServletRequest; @@ -40,7 +39,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Locale; import java.util.Map; -import java.util.Objects; import java.util.TimeZone; import java.util.concurrent.ConcurrentHashMap; @@ -85,9 +83,7 @@ public class ApiArgumentResolver implements SopHandlerMethodArgumentResolver { @Override public boolean supportsParameter(MethodParameter methodParameter) { // 是否有注解 - boolean hasAnnotation = methodParameter.hasMethodAnnotation(ApiMapping.class) - || methodParameter.hasMethodAnnotation(ApiAbility.class); - if (hasAnnotation) { + if (hasApiAnnotation(methodParameter)) { openApiParams.add(methodParameter); } Class paramType = methodParameter.getParameterType(); @@ -116,6 +112,11 @@ public class ApiArgumentResolver implements SopHandlerMethodArgumentResolver { return !exclude; } + private boolean hasApiAnnotation(MethodParameter methodParameter) { + return methodParameter.hasMethodAnnotation(ApiMapping.class) + || OpenUtil.getAnnotationFromMethodOrClass(methodParameter.getMethod(), ApiAbility.class) != null; + } + @Override public Object resolveArgument( MethodParameter methodParameter diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/util/OpenUtil.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/util/OpenUtil.java index 0c77c01f..04a8461e 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/util/OpenUtil.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/util/OpenUtil.java @@ -5,10 +5,13 @@ import com.alibaba.fastjson.JSONObject; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; +import org.springframework.core.annotation.AnnotationUtils; import org.springframework.http.MediaType; import org.springframework.util.DigestUtils; import javax.servlet.http.HttpServletRequest; +import java.lang.annotation.Annotation; +import java.lang.reflect.Method; import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.HashMap; @@ -90,4 +93,22 @@ public class OpenUtil { String serverSign = DigestUtils.md5DigestAsHex(source.getBytes()); return serverSign.equals(sign); } + + /** + * 在方法或方法对应的类上找指定的注解 + * @param method 方法 + * @param annotationClass 指定的注解 + * @param + * @return 返回指定注解,没有返回null + */ + public static T getAnnotationFromMethodOrClass(Method method, Class annotationClass) { + if (method == null) { + return null; + } + T annotation = AnnotationUtils.findAnnotation(method, annotationClass); + if (annotation == null) { + annotation = AnnotationUtils.findAnnotation(method.getDeclaringClass(), annotationClass); + } + return annotation; + } } diff --git a/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/AlipayController2.java b/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/AlipayController2.java new file mode 100644 index 00000000..88629888 --- /dev/null +++ b/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/AlipayController2.java @@ -0,0 +1,32 @@ +package com.gitee.sop.storyweb.controller; + +import com.gitee.sop.servercommon.annotation.ApiAbility; +import com.gitee.sop.storyweb.controller.param.StoryParam; +import com.gitee.sop.storyweb.controller.result.StoryResult; +import io.swagger.annotations.Api; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +/** + * 支付宝服务端,假设签名验证通过后,到达这里进行具体的业务处理。 + * + * @author tanghc + */ +@RestController +@Slf4j +// 注解放在这里,表示类中的方法都具备接口开放能力 +@ApiAbility +public class AlipayController2 { + + // http://localhost:2222/getStory44 + // 遗留接口具备开放平台能力 + @PostMapping("/getStory44") + public StoryResult getStory22(@RequestBody StoryParam param) { + StoryResult story = new StoryResult(); + story.setId(1L); + story.setName("遗留接口44,param:" + param); + return story; + } +}