Add Swagger dependency to pom
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
Add swagger config bean
@Configuration
@EnableSwagger2
public class Swagger2Configuration {
//api接口包扫描路径
public static final String SWAGGER_SCAN_BASE_PACKAGE = "com.lzhenxing.myproject";
public static final String VERSION = "1.0.0";
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BASE_PACKAGE))
.paths(PathSelectors.any()) // 可以根据url路径设置哪些请求加入文档,忽略哪些请求
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("springboot practice") //设置文档的标题
.description("springboot practice API 接口文档") // 设置文档的描述
.version(VERSION) // 设置文档的版本信息
.build();
}
}
swagger test api
@Api("springboot practice API接口")
@RestController
@RequestMapping("/project")
public class SwaggerControllerTest {
@ApiOperation(value="my test", notes="my test", produces="application/json")
@RequestMapping(value="/myTest", method= RequestMethod.GET)
public String myTest() {
return "SUCCESS";
}
}
Start springboot
after starting springboot,visit: http://localhost:8080/swagger-ui.html