一、为工程添加pom依赖:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
二、代码目录:
三、配置swagger具体代码:
package com.we.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;
@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.basePackage("com.we.ui")).paths(PathSelectors.any())//设置扫描的包路径
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("对外API接口").description("对外API接口") //设置描述之类的东西
.termsOfServiceUrl("https://www.jianshu.com/u/7fcaa9fcd2c1").contact("we").version("1.0").build();
}
}
四、使用:
因为设置了要扫描的包路径,所以默认开启该路径下的所有API文档注解,
使用@ApiOperation为接口添加文档注解
使用@ApiIgnore忽略该接口
启动服务,访问: http://localhost:8080/swagger-ui.html 可见到如下页面:
完结。