1.在pom.xml中添加依賴html
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency>
2.建立Swagger2的配置類SwaggerConfigspring
@Configuration //這是一個配置類 @EnableSwagger2 //開啓在線文檔 public class SwaggerConfig { //1.聲明api 文檔的屬性、構建器 private ApiInfo apiInfo(){ return new ApiInfoBuilder().title("SpringBoot中使用在線文檔構建RestFul風格Apis") .description("gxh").contact("gxh").version("1.0").build(); } //2.配置核心配置信息 public Docket createRestApi(){ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select().apis(RequestHandlerSelectors.basePackage("com.offcn.springbootdemo.controller")) .paths(PathSelectors.any()).build(); } }
3.在Controller包下的Controller類上添加文檔註釋api
經過@ApiOperation註解,來給API增長說明;瀏覽器
經過@ApiImplicitParams、@ApiImplicitParam註解,來給參數增長說明;springboot
4.運行,查看生成的Swagger2文檔ui
啓動以後,在瀏覽器中輸入:http://localhost:8080/swagger-ui.htmlspa