1.第一步需要添加maven依赖
<dependencies>
<dependency> <!--添加Web依赖 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency><!--添加Swagger依赖 -->
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency><!--添加Swagger-UI依赖 -->
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
<dependency> <!--添加热部署依赖 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency><!--添加Test依赖 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2.第二步需要配置swagger的配置(config) @Configuration @EnableSwagger2 //声明启动swagger public class SwaggerConfig { @Bean public Docket customDocket() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.alibaba.controller"))//扫描的包路径 .build(); } // 声明api信息 private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("那方面贼猛")//文档说明 .version("2.0.0")//文档版本说明 .build(); } }
3.第三步就是需要再conroller层配置swagger注解,说明每个接口的功能
4.第四步就可以启动项目访问swagger的web页面的了 注意访问路径为:http://ip:端口号/swagger-ui.html