From 801b5f1fd48b33805bdebe6569c8b19f42b3a0c2 Mon Sep 17 00:00:00 2001 From: tanghc Date: Sat, 9 May 2020 16:53:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84springmvc=20demo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../configuration/SpringMvcServiceConfiguration.java | 10 +++++++++- .../servercommon/mapping/ApiMappingHandlerMapping.java | 3 +-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/SpringMvcServiceConfiguration.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/SpringMvcServiceConfiguration.java index af4ed868..d5bb8bdc 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/SpringMvcServiceConfiguration.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/SpringMvcServiceConfiguration.java @@ -6,12 +6,14 @@ import com.gitee.sop.servercommon.manager.EnvironmentContext; import com.gitee.sop.servercommon.manager.ServiceRouteController; import com.gitee.sop.servercommon.mapping.ApiMappingHandlerMapping; import com.gitee.sop.servercommon.message.ServiceErrorFactory; +import com.gitee.sop.servercommon.param.SopHandlerMethodArgumentResolver; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Primary; import org.springframework.core.env.Environment; +import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; import javax.annotation.PostConstruct; @@ -25,6 +27,8 @@ public class SpringMvcServiceConfiguration { static { System.setProperty(ServiceContext.SOP_MVC, "true"); + // 默认版本号为1.0 + ServiceConfig.getInstance().setDefaultVersion("1.0"); } public SpringMvcServiceConfiguration() { @@ -33,6 +37,9 @@ public class SpringMvcServiceConfiguration { private ApiMappingHandlerMapping apiMappingHandlerMapping = new ApiMappingHandlerMapping(); + @Autowired + private RequestMappingHandlerAdapter requestMappingHandlerAdapter; + @Autowired private Environment environment; @@ -72,7 +79,8 @@ public class SpringMvcServiceConfiguration { * spring容器加载完毕后执行 */ protected void doAfter() { - + SopHandlerMethodArgumentResolver sopHandlerMethodArgumentResolver = ServiceConfig.getInstance().getMethodArgumentResolver(); + sopHandlerMethodArgumentResolver.setRequestMappingHandlerAdapter(requestMappingHandlerAdapter); } protected void initMessage() { 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 1d706005..1a3696b3 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 @@ -5,7 +5,6 @@ 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.apache.commons.lang.StringUtils; import org.springframework.core.PriorityOrdered; import org.springframework.util.StringValueResolver; import org.springframework.web.servlet.mvc.condition.RequestCondition; @@ -30,7 +29,7 @@ public class ApiMappingHandlerMapping extends RequestMappingHandlerMapping imple StringValueResolver valueResolver = null; if (apiMapping != null) { String version = apiMapping.version(); - if (StringUtils.isBlank(version)) { + if ("".equals(version)) { version = ServiceConfig.getInstance().getDefaultVersion(); } valueResolver = new ApiMappingStringValueResolverVersion(version);