一、引入包,pom.XML配置如下
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>
二、编写配置类
package com.anjiplus.template.gaea.business.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; 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; import springfox.documentation.swagger2.annotations.EnableSwagger2; /** * @Author 尹明能 * @Date 2022/4/12 12:52 * @Version 1.0 */ @EnableSwagger2 @Configuration public class Swagger2Config { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.anjiplus.template.gaea.business.modules")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("利用Swagger2构建RESTful APIs") .description("swagger demo") .termsOfServiceUrl("") .version("1.0") .build(); } }
三、拦截规则配置,否则会出现swagger打开白屏问题
这个是我自己写的规则,如果使用spring securiry参考其他方式
kip-authenticate-urls: /gaeaDict/all, /login, /static, /file/download/, /index.html,/swagger,/webjars,/v2,/swagger-resources,/swagger-ui.html, /favicon.ico, /reportShare/detailByCode,/reportDashboard
四、控制器增加@API注解