SpringBoot集成swagger2html
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version> </dependency>
在Application類上添加@EnableSwagger2註解 如: @EnableSwagger2 @SpringBootApplication public class SpringbootDemoApplication
如: @ApiOperation(value = "獲取用戶詳細信息", notes = "根據url的id來獲取用戶詳細信息") @ApiImplicitParam(name = "id", value = "用戶ID", required = true, dataType = "Integer", paramType = "path") @GetMapping("/user/{id}") public User getUserById(@PathVariable("id") Integer id) { User user = DATA.get(id); if (user == null) { user = new User(0, "moringcat"); } return user; }
http://127.0.0.1:8080/swagger-ui.html
@Api:修飾整個類,描述Controller的做用 @ApiOperation:描述一個類的一個方法,或者說一個接口 @ApiParam:單個參數描述 @ApiModel:用對象來接收參數 @ApiProperty:用對象接收參數時,描述對象的一個字段 @ApiResponse:HTTP響應其中1個描述 @ApiResponses:HTTP響應總體描述 @ApiIgnore:使用該註解忽略這個API @ApiError :發生錯誤返回的信息 @ApiImplicitParam:一個請求參數 @ApiImplicitParams:多個請求參數
demo地址:https://gitee.com/mengzhang6/springboot-swagger2-demo git