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