restful调用优化

This commit is contained in:
tanghc
2019-10-15 12:41:02 +08:00
parent 6b3b38e058
commit 58cd959e01
6 changed files with 126 additions and 62 deletions

View File

@@ -2,14 +2,15 @@ package com.gitee.sop.servercommon.configuration;
import com.gitee.sop.servercommon.bean.ServiceConfig;
import com.gitee.sop.servercommon.interceptor.ServiceContextInterceptor;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
@@ -25,8 +26,7 @@ import java.util.List;
* @author tanghc
*/
@Slf4j
public class BaseServiceConfiguration extends WebMvcConfigurationSupport
implements ApplicationRunner {
public class BaseServiceConfiguration extends WebMvcConfigurationSupport {
public BaseServiceConfiguration() {
ServiceConfig.getInstance().getI18nModules().add("i18n/isp/bizerror");
@@ -34,6 +34,9 @@ public class BaseServiceConfiguration extends WebMvcConfigurationSupport
private ApiMappingHandlerMapping apiMappingHandlerMapping = new ApiMappingHandlerMapping();
@Autowired
private Environment environment;
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
super.addResourceHandlers(registry);
@@ -85,20 +88,11 @@ public class BaseServiceConfiguration extends WebMvcConfigurationSupport
@PostConstruct
public final void after() {
log.info("-----spring容器加载完毕-----");
EnvironmentContext.setEnvironment(environment);
initMessage();
doAfter();
}
/**
* springboot启动完成后执行
* @param args 启动参数
* @throws Exception 出错异常
*/
@Override
public void run(ApplicationArguments args) throws Exception {
log.info("-----服务器启动完毕-----");
this.onStartup(args);
}
/**
* spring容器加载完毕后执行
@@ -107,13 +101,6 @@ public class BaseServiceConfiguration extends WebMvcConfigurationSupport
}
/**
* 启动完毕后执行
* @param args
*/
protected void onStartup(ApplicationArguments args) {
}
protected void initMessage() {
ServiceErrorFactory.initMessageSource(ServiceConfig.getInstance().getI18nModules());

View File

@@ -7,6 +7,7 @@ import com.gitee.sop.servercommon.mapping.ApiMappingInfo;
import com.gitee.sop.servercommon.mapping.ApiMappingRequestCondition;
import com.gitee.sop.servercommon.mapping.RouteUtil;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.condition.RequestCondition;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
@@ -91,11 +92,20 @@ public class ApiMetaBuilder {
if (!ServiceContext.getCurrentContext().getBoolean(ServiceContext.RESTFUL_KEY, false)) {
return null;
}
// 如果是restful服务
String path = patterns.iterator().next();
if (path.contains("$") || isIgnorePattern(path)) {
return null;
}
ServiceApiInfo.ApiMeta apiMeta = new ServiceApiInfo.ApiMeta(path, path, "");
String name = path;
String prefix = EnvironmentKeys.SOP_RESTFUL_PREFIX.getValue();
if (StringUtils.isEmpty(prefix)) {
prefix = EnvironmentKeys.SPRING_APPLICATION_NAME.getValue();
}
if (StringUtils.hasText(prefix)) {
name = "/" + prefix + "/" + StringUtils.trimLeadingCharacter(path, '/');
}
ServiceApiInfo.ApiMeta apiMeta = new ServiceApiInfo.ApiMeta(name, path, "");
apiMeta.setIgnoreValidate(BooleanUtils.toInteger(true));
apiMeta.setMergeResult(BooleanUtils.toInteger(false));
apiMeta.setPermission(BooleanUtils.toInteger(false));

View File

@@ -0,0 +1,23 @@
package com.gitee.sop.servercommon.manager;
import org.springframework.core.env.Environment;
/**
* @author tanghc
*/
public class EnvironmentContext {
private static Environment environment;
public static Environment getEnvironment() {
return environment;
}
public static void setEnvironment(Environment environment) {
EnvironmentContext.environment = environment;
}
public static String getValue(String key, String defaultValue) {
return environment.getProperty(key, defaultValue);
}
}

View File

@@ -0,0 +1,37 @@
package com.gitee.sop.servercommon.manager;
import javax.annotation.Nullable;
public enum EnvironmentKeys {
SPRING_PROFILES_ACTIVE("spring.profiles.active", "default"),
/**
* spring.application.name
*/
SPRING_APPLICATION_NAME("spring.application.name"),
/**
* sop.restful.prefix=xxx指定web开发模式前缀不指定默认为service-id
*/
SOP_RESTFUL_PREFIX("sop.restful.prefix");
private String key;
private String defaultValue;
public String getKey() {
return key;
}
EnvironmentKeys(String key) {
this.key = key;
}
EnvironmentKeys(String key, String defaultValue) {
this.key = key;
this.defaultValue = defaultValue;
}
@Nullable
public String getValue() {
return EnvironmentContext.getValue(key, defaultValue);
}
}