| | |
| | | package com.vincent.rsf.server.common.config; |
| | | |
| | | import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiModel; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import org.springframework.beans.BeansException; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.beans.factory.config.BeanPostProcessor; |
| | | import io.swagger.v3.oas.models.OpenAPI; |
| | | import io.swagger.v3.oas.models.info.Info; |
| | | import org.springdoc.core.models.GroupedOpenApi; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | import org.springframework.util.ReflectionUtils; |
| | | import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping; |
| | | import springfox.documentation.builders.ApiInfoBuilder; |
| | | import springfox.documentation.builders.PathSelectors; |
| | | import springfox.documentation.builders.RequestHandlerSelectors; |
| | | import springfox.documentation.oas.annotations.EnableOpenApi; |
| | | import springfox.documentation.service.ApiInfo; |
| | | import springfox.documentation.spi.DocumentationType; |
| | | import springfox.documentation.spring.web.plugins.Docket; |
| | | import springfox.documentation.spring.web.plugins.WebFluxRequestHandlerProvider; |
| | | import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider; |
| | | import java.lang.reflect.Field; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | |
| | | |
| | | @Configuration |
| | | @EnableKnife4j |
| | | @EnableOpenApi |
| | | public class SwaggerConfig { |
| | | |
| | | @Value("${spring.profiles.active}") |
| | | private String active; |
| | | |
| | | @Bean |
| | | public Docket docket() { |
| | | Docket docket = new Docket(DocumentationType.OAS_30) |
| | | .apiInfo(apiInfo()) |
| | | .enable("dev".equals(active)) |
| | | .groupName("测试分组") |
| | | .select() |
| | | //apis: 添加swagger接口提取范围 |
| | | .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) |
| | | // .apis(RequestHandlerSelectors.basePackage("com.vincent.rsf.server.manager.controller")) |
| | | .paths(PathSelectors.any()) |
| | | .build(); |
| | | |
| | | return docket; |
| | | } |
| | | |
| | | @Bean |
| | | public Docket erpDocket() { |
| | | Docket docket = new Docket(DocumentationType.OAS_30) |
| | | .apiInfo(apiInfo()) |
| | | .enable("dev".equals(active)) |
| | | .groupName("ERP接口对接") |
| | | .select() |
| | | //apis: 添加swagger接口提取范围 |
| | | .apis(RequestHandlerSelectors.basePackage("com.vincent.rsf.server.api.controller.erp")) |
| | | .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) |
| | | .paths(PathSelectors.any()) |
| | | .build(); |
| | | |
| | | return docket; |
| | | } |
| | | |
| | | |
| | | private ApiInfo apiInfo() { |
| | | return new ApiInfoBuilder() |
| | | public OpenAPI rsfOpenApi() { |
| | | return new OpenAPI().info(new Info() |
| | | .title("WMS标版1.0版本接口文档") |
| | | .description("WMS标版1.0版本接口文档,技术栈主要包括:SpringBoot、React.js、MySQL5.7") |
| | | .version("v1.0") |
| | | .version("v1.0")); |
| | | } |
| | | |
| | | @Bean |
| | | public GroupedOpenApi defaultApi() { |
| | | return GroupedOpenApi.builder() |
| | | .group("测试分组") |
| | | .pathsToMatch("/**") |
| | | .pathsToExclude("/erp/**") |
| | | .build(); |
| | | } |
| | | |
| | | @Bean |
| | | public static BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() { |
| | | return new BeanPostProcessor() { |
| | | |
| | | @Override |
| | | public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { |
| | | if (bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider) { |
| | | customizeSpringfoxHandlerMappings(getHandlerMappings(bean)); |
| | | } |
| | | return bean; |
| | | } |
| | | |
| | | private <T extends RequestMappingInfoHandlerMapping> void customizeSpringfoxHandlerMappings(List<T> mappings) { |
| | | List<T> copy = mappings.stream() |
| | | .filter(mapping -> mapping.getPatternParser() == null) |
| | | .collect(Collectors.toList()); |
| | | mappings.clear(); |
| | | mappings.addAll(copy); |
| | | } |
| | | |
| | | @SuppressWarnings("unchecked") |
| | | private List<RequestMappingInfoHandlerMapping> getHandlerMappings(Object bean) { |
| | | try { |
| | | Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings"); |
| | | field.setAccessible(true); |
| | | return (List<RequestMappingInfoHandlerMapping>) field.get(bean); |
| | | } catch (IllegalArgumentException | IllegalAccessException e) { |
| | | throw new IllegalStateException(e); |
| | | } |
| | | } |
| | | }; |
| | | public GroupedOpenApi erpApi() { |
| | | return GroupedOpenApi.builder() |
| | | .group("ERP接口对接") |
| | | .pathsToMatch("/erp/**") |
| | | .build(); |
| | | } |
| | | |
| | | } |