| | |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | import springfox.documentation.builders.ApiInfoBuilder; |
| | | import springfox.documentation.builders.ParameterBuilder; |
| | | import springfox.documentation.builders.PathSelectors; |
| | | import springfox.documentation.builders.RequestHandlerSelectors; |
| | | import springfox.documentation.schema.ModelRef; |
| | | import springfox.documentation.service.*; |
| | | import springfox.documentation.service.ApiInfo; |
| | | import springfox.documentation.service.Contact; |
| | | import springfox.documentation.spi.DocumentationType; |
| | | import springfox.documentation.spi.service.contexts.SecurityContext; |
| | | import springfox.documentation.spring.web.plugins.Docket; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.Collections; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * @author zhan_py |
| | |
| | | @Configuration |
| | | public class Swagger2Config { |
| | | |
| | | |
| | | @Bean |
| | | public Docket webApiConfig() { |
| | | List<Parameter> pars = new ArrayList<>(); |
| | | ParameterBuilder tokenPar = new ParameterBuilder(); |
| | | tokenPar.name("userId") |
| | | .description("用户token") |
| | | //.defaultValue(JwtHelper.createToken(1L, "admin")) |
| | | .defaultValue("1") |
| | | .modelRef(new ModelRef("string")) |
| | | .parameterType("header") |
| | | .required(false) |
| | | .build(); |
| | | pars.add(tokenPar.build()); |
| | | public Docket moduleDocket() { |
| | | return docket("mes接口文档", "com.mes"); |
| | | } |
| | | |
| | | |
| | | private Docket docket(String groupName, String basePackages) { |
| | | return new Docket(DocumentationType.SWAGGER_2) |
| | | .groupName("webApi") |
| | | .apiInfo(webApiInfo()) |
| | | .groupName(groupName) |
| | | .apiInfo(apiInfo()) |
| | | .select() |
| | | //只显示api路径下的页面 |
| | | .apis(RequestHandlerSelectors.basePackage("com.mes")) |
| | | .apis(RequestHandlerSelectors.basePackage(basePackages)) |
| | | .paths(PathSelectors.any()) |
| | | .build() |
| | | .globalOperationParameters(pars) |
| | | .securitySchemes(Collections.EMPTY_LIST) |
| | | .securityContexts(Arrays.asList(securityContext())); |
| | | } |
| | | |
| | | private SecurityContext securityContext() { |
| | | return SecurityContext.builder() |
| | | .securityReferences(defaultAuth()) |
| | | .forPaths(PathSelectors.any()) |
| | | .build(); |
| | | } |
| | | |
| | | private List<SecurityReference> defaultAuth() { |
| | | AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything"); |
| | | AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; |
| | | authorizationScopes[0] = authorizationScope; |
| | | return Arrays.asList(new SecurityReference("Authorization", authorizationScopes)); |
| | | } |
| | | |
| | | |
| | | private ApiInfo webApiInfo() { |
| | | private ApiInfo apiInfo() { |
| | | return new ApiInfoBuilder() |
| | | .title("网站-API文档") |
| | | .description("本文档描述了mes网站微服务接口定义") |
| | | .version("1.0") |
| | | .contact(new Contact("zhan_py", "", "")) |
| | | .title("mes接口文档系统") |
| | | .description("mesApi接口文档系统") |
| | | .license("Powered By mes") |
| | | .licenseUrl("http://127.0.0.1") |
| | | .termsOfServiceUrl("http://127.0.0.1") |
| | | .contact(new Contact("mes", "http://127.0.0.1", "beibo@mes.com")) |
| | | .version("V1.0.0") |
| | | .build(); |
| | | } |
| | | |
| | | |
| | | // @Bean |
| | | // public Docket webApiConfig() { |
| | | // List<Parameter> pars = new ArrayList<>(); |
| | | // ParameterBuilder tokenPar = new ParameterBuilder(); |
| | | // tokenPar.name("userId") |
| | | // .description("用户token") |
| | | // .defaultValue("1") |
| | | // .modelRef(new ModelRef("string")) |
| | | // .parameterType("header") |
| | | // .required(false) |
| | | // .build(); |
| | | // pars.add(tokenPar.build()); |
| | | // |
| | | // return new Docket(DocumentationType.SWAGGER_2) |
| | | // .apiInfo(webApiInfo()) |
| | | // .select() |
| | | // //只显示api路径下的页面 |
| | | // .apis(RequestHandlerSelectors.basePackage("com.mes")) |
| | | // .paths(PathSelectors.any()) |
| | | // .build() |
| | | // .globalOperationParameters(pars) |
| | | // .securitySchemes(Collections.EMPTY_LIST) |
| | | // .securityContexts(Arrays.asList(securityContext())); |
| | | // } |
| | | // |
| | | // private SecurityContext securityContext() { |
| | | // return SecurityContext.builder() |
| | | // .securityReferences(defaultAuth()) |
| | | // .forPaths(PathSelectors.any()) |
| | | // .build(); |
| | | // } |
| | | // |
| | | // private List<SecurityReference> defaultAuth() { |
| | | // AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything"); |
| | | // AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; |
| | | // authorizationScopes[0] = authorizationScope; |
| | | // return Arrays.asList(new SecurityReference("Authorization", authorizationScopes)); |
| | | // } |
| | | // |
| | | // |
| | | // private ApiInfo webApiInfo() { |
| | | // return new ApiInfoBuilder() |
| | | // .title("网站-API文档") |
| | | // .description("本文档描述了mes网站微服务接口定义") |
| | | // .version("1.0") |
| | | // .contact(new Contact("zhan_py", "", "")) |
| | | // .build(); |
| | | // } |
| | | } |