pull/1/head
tanghc 5 years ago
parent 51c4c7a82d
commit 0966784976
  1. 43
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/param/ApiArgumentResolver.java
  2. 3
      sop-example/sop-book/sop-book-web/src/main/resources/application-dev.properties
  3. 1
      sop-website/src/main/java/com/gitee/sop/websiteserver/manager/DocManagerImpl.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> T resolveNativeRequest(NativeWebRequest webRequest, Class<T> 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> T resolveNativeResponse(NativeWebRequest webRequest, Class<T> 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 参数

@ -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,数字越大越耗性能

@ -77,6 +77,7 @@ public class DocManagerImpl implements DocManager, ApplicationListener<Heartbeat
@Override
public void remove(String serviceId) {
serviceIdMd5Map.remove(serviceId);
docDefinitionMap.entrySet().removeIf(entry -> serviceId.equalsIgnoreCase(entry.getValue().getServiceId()));
}

Loading…
Cancel
Save