From 0966784976e5ea29d8295ddd652feb0a5ae1ae30 Mon Sep 17 00:00:00 2001 From: tanghc Date: Mon, 26 Aug 2019 14:58:20 +0800 Subject: [PATCH] 2.0 --- .../param/ApiArgumentResolver.java | 43 ++++++++++++++++++- .../main/resources/application-dev.properties | 3 ++ .../websiteserver/manager/DocManagerImpl.java | 1 + 3 files changed, 46 insertions(+), 1 deletion(-) 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 68dafc12..2de4034d 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 @@ -12,13 +12,17 @@ import org.springframework.core.MethodParameter; import org.springframework.util.ReflectionUtils; import org.springframework.web.bind.support.WebDataBinderFactory; import org.springframework.web.context.request.NativeWebRequest; +import org.springframework.web.context.request.WebRequest; import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.method.support.ModelAndViewContainer; 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 javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; @@ -95,9 +99,28 @@ public class ApiArgumentResolver implements SopHandlerMethodArgumentResolver { * @return 返回参数绑定的对象,没有返回null */ protected Object getParamObject(MethodParameter methodParameter, NativeWebRequest nativeWebRequest) { + Class parameterType = methodParameter.getParameterType(); + // WebRequest / NativeWebRequest / ServletWebRequest + if (WebRequest.class.isAssignableFrom(parameterType)) { + if (!parameterType.isInstance(nativeWebRequest)) { + throw new IllegalStateException( + "Current request is not of type [" + parameterType.getName() + "]: " + nativeWebRequest); + } + return nativeWebRequest; + } + + // ServletRequest / HttpServletRequest / MultipartRequest / MultipartHttpServletRequest + if (ServletRequest.class.isAssignableFrom(parameterType) || MultipartRequest.class.isAssignableFrom(parameterType)) { + return resolveNativeRequest(nativeWebRequest, parameterType); + } + + // ServletResponse, HttpServletResponse + if (ServletResponse.class.isAssignableFrom(parameterType)) { + return resolveNativeResponse(nativeWebRequest, parameterType); + } + HttpServletRequest request = (HttpServletRequest) nativeWebRequest.getNativeRequest(); JSONObject requestParams = OpenUtil.getRequestParams(request); - Class parameterType = methodParameter.getParameterType(); // 方法参数类型 Class bizObjClass = parameterType; boolean isOpenRequestParam = parameterType == OpenContext.class; @@ -112,6 +135,24 @@ public class ApiArgumentResolver implements SopHandlerMethodArgumentResolver { return isOpenRequestParam ? openContext : bizObj; } + private T resolveNativeRequest(NativeWebRequest webRequest, Class requiredType) { + T nativeRequest = webRequest.getNativeRequest(requiredType); + if (nativeRequest == null) { + throw new IllegalStateException( + "Current request is not of type [" + requiredType.getName() + "]: " + webRequest); + } + return nativeRequest; + } + + private T resolveNativeResponse(NativeWebRequest webRequest, Class requiredType) { + T nativeResponse = webRequest.getNativeResponse(requiredType); + if (nativeResponse == null) { + throw new IllegalStateException( + "Current response is not of type [" + requiredType.getName() + "]: " + webRequest); + } + return nativeResponse; + } + /** * 获取泛型参数类型 * @param methodParameter 参数 diff --git a/sop-example/sop-book/sop-book-web/src/main/resources/application-dev.properties b/sop-example/sop-book/sop-book-web/src/main/resources/application-dev.properties index 9f2eb875..05644cb7 100644 --- a/sop-example/sop-book/sop-book-web/src/main/resources/application-dev.properties +++ b/sop-example/sop-book/sop-book-web/src/main/resources/application-dev.properties @@ -4,6 +4,9 @@ spring.application.name=book-service # nacos注册中心 spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 +# consumer不需要检查provider是否启动 +dubbo.consumer.check=false + # zipkin服务跟踪 #spring.zipkin.base-url=http://127.0.0.1:9411/ ## 设置sleuth收集信息的比率,默认0.1,最大是1,数字越大越耗性能 diff --git a/sop-website/src/main/java/com/gitee/sop/websiteserver/manager/DocManagerImpl.java b/sop-website/src/main/java/com/gitee/sop/websiteserver/manager/DocManagerImpl.java index 1c562b6f..9d7e3114 100644 --- a/sop-website/src/main/java/com/gitee/sop/websiteserver/manager/DocManagerImpl.java +++ b/sop-website/src/main/java/com/gitee/sop/websiteserver/manager/DocManagerImpl.java @@ -77,6 +77,7 @@ public class DocManagerImpl implements DocManager, ApplicationListener serviceId.equalsIgnoreCase(entry.getValue().getServiceId())); }