parent
bb96c47860
commit
e28fd3b2ec
@ -0,0 +1,27 @@ |
|||||||
|
package com.gitee.sop.servercommon.bean; |
||||||
|
|
||||||
|
import org.springframework.core.env.Environment; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author thc |
||||||
|
*/ |
||||||
|
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 getProfile(Environment env) { |
||||||
|
return env.getProperty("spring.profiles.active", "default"); |
||||||
|
} |
||||||
|
|
||||||
|
public static String getProfile() { |
||||||
|
return getProfile(environment); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,84 @@ |
|||||||
|
package com.gitee.sop.servercommon.configuration; |
||||||
|
|
||||||
|
import com.gitee.sop.servercommon.bean.EnvironmentContext; |
||||||
|
import com.gitee.sop.servercommon.bean.ServiceConfig; |
||||||
|
import com.gitee.sop.servercommon.manager.ApiMetaManager; |
||||||
|
import com.gitee.sop.servercommon.manager.DefaultRequestMappingEvent; |
||||||
|
import com.gitee.sop.servercommon.manager.RequestMappingEvent; |
||||||
|
import com.gitee.sop.servercommon.manager.ServiceZookeeperApiMetaManager; |
||||||
|
import com.gitee.sop.servercommon.mapping.ApiMappingHandlerMapping; |
||||||
|
import com.gitee.sop.servercommon.message.ServiceErrorFactory; |
||||||
|
import lombok.extern.slf4j.Slf4j; |
||||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||||
|
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.RequestMappingHandlerMapping; |
||||||
|
|
||||||
|
import javax.annotation.PostConstruct; |
||||||
|
import java.util.concurrent.Executors; |
||||||
|
|
||||||
|
/** |
||||||
|
* 提供给springmvc工程 |
||||||
|
* @author tanghc |
||||||
|
*/ |
||||||
|
@Slf4j |
||||||
|
public class SpringMvcServiceConfiguration { |
||||||
|
|
||||||
|
public SpringMvcServiceConfiguration() { |
||||||
|
ServiceConfig.getInstance().getI18nModules().add("i18n/isp/bizerror"); |
||||||
|
} |
||||||
|
|
||||||
|
private ApiMappingHandlerMapping apiMappingHandlerMapping = new ApiMappingHandlerMapping(); |
||||||
|
|
||||||
|
@Autowired |
||||||
|
private Environment environment; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* 自定义Mapping,详见@ApiMapping |
||||||
|
* |
||||||
|
* @return 返回RequestMappingHandlerMapping |
||||||
|
*/ |
||||||
|
@Bean |
||||||
|
@Primary |
||||||
|
public RequestMappingHandlerMapping requestMappingHandlerMapping() { |
||||||
|
return apiMappingHandlerMapping; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
@Bean |
||||||
|
GlobalExceptionHandler globalExceptionHandler() { |
||||||
|
return ServiceConfig.getInstance().getGlobalExceptionHandler(); |
||||||
|
} |
||||||
|
|
||||||
|
@PostConstruct |
||||||
|
public final void after() { |
||||||
|
log.info("-----spring容器加载完毕-----"); |
||||||
|
EnvironmentContext.setEnvironment(environment); |
||||||
|
Executors.newSingleThreadExecutor().execute(()->{ |
||||||
|
uploadRouteToZookeeper(); |
||||||
|
}); |
||||||
|
initMessage(); |
||||||
|
doAfter(); |
||||||
|
} |
||||||
|
|
||||||
|
private void uploadRouteToZookeeper() { |
||||||
|
ApiMetaManager apiMetaManager = new ServiceZookeeperApiMetaManager(environment); |
||||||
|
RequestMappingEvent requestMappingEvent = new DefaultRequestMappingEvent(apiMetaManager, environment); |
||||||
|
requestMappingEvent.onRegisterSuccess(apiMappingHandlerMapping); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* spring容器加载完毕后执行 |
||||||
|
*/ |
||||||
|
protected void doAfter() { |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
protected void initMessage() { |
||||||
|
ServiceErrorFactory.initMessageSource(ServiceConfig.getInstance().getI18nModules()); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,20 @@ |
|||||||
|
package com.gitee.sop.servercommon.mapping; |
||||||
|
|
||||||
|
import org.springframework.util.StringUtils; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author tanghc |
||||||
|
*/ |
||||||
|
public class MappingUtil { |
||||||
|
/** |
||||||
|
* 将springmvc接口路径转换成SOP方法名 |
||||||
|
* @param path springmvc路径,如/a/b,/goods/listGoods |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
public static String buildApiName(String path) { |
||||||
|
path = StringUtils.trimLeadingCharacter(path, '/'); |
||||||
|
path = StringUtils.trimTrailingCharacter(path, '/'); |
||||||
|
path = path.replace("/", "."); |
||||||
|
return path; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,16 @@ |
|||||||
|
package com.gitee.sop.servercommon.swagger; |
||||||
|
|
||||||
|
import springfox.documentation.spring.web.plugins.DocumentationPluginsManager; |
||||||
|
import springfox.documentation.spring.web.scanners.ApiDescriptionReader; |
||||||
|
import springfox.documentation.spring.web.scanners.ApiListingScanner; |
||||||
|
import springfox.documentation.spring.web.scanners.ApiModelReader; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author tanghc |
||||||
|
*/ |
||||||
|
public class ApiListingScannerExt extends ApiListingScanner { |
||||||
|
public ApiListingScannerExt(ApiDescriptionReader apiDescriptionReader, ApiModelReader apiModelReader, DocumentationPluginsManager pluginsManager) { |
||||||
|
super(apiDescriptionReader, apiModelReader, pluginsManager); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,60 @@ |
|||||||
|
package com.gitee.sop.servercommon.swagger; |
||||||
|
|
||||||
|
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.mapping.MappingUtil; |
||||||
|
import com.google.common.base.Optional; |
||||||
|
import springfox.documentation.service.Operation; |
||||||
|
import springfox.documentation.service.StringVendorExtension; |
||||||
|
import springfox.documentation.service.VendorExtension; |
||||||
|
import springfox.documentation.spi.service.contexts.OperationContext; |
||||||
|
import springfox.documentation.spring.web.plugins.DocumentationPluginsManager; |
||||||
|
|
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author tanghc |
||||||
|
*/ |
||||||
|
public class DocumentationPluginsManagerExt extends DocumentationPluginsManager { |
||||||
|
|
||||||
|
public static final String SOP_NAME = "sop_name"; |
||||||
|
public static final String SOP_VERSION = "sop_version"; |
||||||
|
|
||||||
|
@Override |
||||||
|
public Operation operation(OperationContext operationContext) { |
||||||
|
Operation operation = super.operation(operationContext); |
||||||
|
this.setVendorExtension(operation, operationContext); |
||||||
|
return operation; |
||||||
|
} |
||||||
|
|
||||||
|
private void setVendorExtension(Operation operation, OperationContext operationContext) { |
||||||
|
List<VendorExtension> vendorExtensions = operation.getVendorExtensions(); |
||||||
|
Optional<ApiMapping> mappingOptional = operationContext.findAnnotation(ApiMapping.class); |
||||||
|
if (mappingOptional.isPresent()) { |
||||||
|
ApiMapping apiMapping = mappingOptional.get(); |
||||||
|
String name = apiMapping.value()[0]; |
||||||
|
String version = buildVersion(apiMapping.version()); |
||||||
|
vendorExtensions.add(new StringVendorExtension(SOP_NAME, name)); |
||||||
|
vendorExtensions.add(new StringVendorExtension(SOP_VERSION, version)); |
||||||
|
} else { |
||||||
|
Optional<ApiAbility> abilityOptional = operationContext.findAnnotation(ApiAbility.class); |
||||||
|
if (abilityOptional.isPresent()) { |
||||||
|
ApiAbility apiAbility = abilityOptional.get(); |
||||||
|
String mappingPattern = operationContext.requestMappingPattern(); |
||||||
|
String name = MappingUtil.buildApiName(mappingPattern); |
||||||
|
String version = buildVersion(apiAbility.version()); |
||||||
|
vendorExtensions.add(new StringVendorExtension(SOP_NAME, name)); |
||||||
|
vendorExtensions.add(new StringVendorExtension(SOP_VERSION, version)); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private String buildVersion(String version) { |
||||||
|
if ("".equals(version)) { |
||||||
|
return ServiceConfig.getInstance().getDefaultVersion(); |
||||||
|
} else { |
||||||
|
return version; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,44 @@ |
|||||||
|
package com.gitee.sop.servercommon.swagger; |
||||||
|
|
||||||
|
import io.swagger.annotations.ApiOperation; |
||||||
|
import org.springframework.context.annotation.Bean; |
||||||
|
import org.springframework.context.annotation.Primary; |
||||||
|
import springfox.documentation.builders.ApiInfoBuilder; |
||||||
|
import springfox.documentation.builders.PathSelectors; |
||||||
|
import springfox.documentation.builders.RequestHandlerSelectors; |
||||||
|
import springfox.documentation.service.ApiInfo; |
||||||
|
import springfox.documentation.spi.DocumentationType; |
||||||
|
import springfox.documentation.spring.web.plugins.Docket; |
||||||
|
|
||||||
|
public abstract class SwaggerSupport { |
||||||
|
|
||||||
|
protected abstract String getDocTitle(); |
||||||
|
|
||||||
|
@Bean |
||||||
|
@Primary |
||||||
|
public DocumentationPluginsManagerExt documentationPluginsManagerExt() { |
||||||
|
return new DocumentationPluginsManagerExt(); |
||||||
|
} |
||||||
|
|
||||||
|
@Bean |
||||||
|
public Docket createRestApi() { |
||||||
|
return new Docket(DocumentationType.SWAGGER_2) |
||||||
|
.apiInfo(apiInfo()) |
||||||
|
.select() |
||||||
|
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) |
||||||
|
.paths(PathSelectors.any()) |
||||||
|
.build(); |
||||||
|
} |
||||||
|
|
||||||
|
protected ApiInfo apiInfo() { |
||||||
|
return new ApiInfoBuilder() |
||||||
|
.title(getDocTitle()) |
||||||
|
.description("文档描述") |
||||||
|
.termsOfServiceUrl("文档") |
||||||
|
.version("1.0") |
||||||
|
.build(); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} |
@ -1,20 +1,15 @@ |
|||||||
package com.gitee.app.config; |
package com.gitee.app.config; |
||||||
|
|
||||||
import com.gitee.sop.servercommon.configuration.AlipayServiceConfiguration; |
import com.gitee.sop.servercommon.bean.ServiceConfig; |
||||||
import org.springframework.context.annotation.Bean; |
import com.gitee.sop.servercommon.configuration.SpringMvcServiceConfiguration; |
||||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; |
|
||||||
|
|
||||||
/** |
/** |
||||||
* 使用支付宝开放平台功能 |
* 使用支付宝开放平台功能 |
||||||
* |
* |
||||||
* @author tanghc |
* @author tanghc |
||||||
*/ |
*/ |
||||||
public class OpenServiceConfig extends AlipayServiceConfiguration { |
public class OpenServiceConfig extends SpringMvcServiceConfiguration { |
||||||
|
static { |
||||||
@Bean |
ServiceConfig.getInstance().setDefaultVersion("1.0"); |
||||||
@Override |
|
||||||
public RequestMappingHandlerMapping requestMappingHandlerMapping() { |
|
||||||
return super.requestMappingHandlerMapping(); |
|
||||||
} |
} |
||||||
|
|
||||||
} |
} |
||||||
|
@ -0,0 +1,18 @@ |
|||||||
|
package com.gitee.sop.bookweb.controller.param; |
||||||
|
|
||||||
|
import io.swagger.annotations.ApiModelProperty; |
||||||
|
import lombok.Data; |
||||||
|
import org.hibernate.validator.constraints.Length; |
||||||
|
|
||||||
|
import javax.validation.constraints.NotBlank; |
||||||
|
|
||||||
|
@Data |
||||||
|
public class StoryParam { |
||||||
|
@ApiModelProperty(value = "故事ID", example = "111") |
||||||
|
private int id; |
||||||
|
|
||||||
|
@NotBlank(message = "name不能为空") |
||||||
|
@Length(max = 20, message = "name长度不能超过20") |
||||||
|
@ApiModelProperty(value = "故事名称", required = true, example = "白雪公主") |
||||||
|
private String name; |
||||||
|
} |
Loading…
Reference in new issue