优化参数绑定

2.x
tanghc 5 years ago
parent 49615e6476
commit 05395ea0b9
  1. 14
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/ApiMappingHandlerMapping.java
  2. 11
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/param/ApiArgumentResolver.java
  3. 21
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/util/OpenUtil.java
  4. 32
      sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/AlipayController2.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;
}
}

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

@ -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 <T>
* @return 返回指定注解没有返回null
*/
public static <T extends Annotation> T getAnnotationFromMethodOrClass(Method method, Class<T> annotationClass) {
if (method == null) {
return null;
}
T annotation = AnnotationUtils.findAnnotation(method, annotationClass);
if (annotation == null) {
annotation = AnnotationUtils.findAnnotation(method.getDeclaringClass(), annotationClass);
}
return annotation;
}
}

@ -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;
}
}
Loading…
Cancel
Save