SpringBoot集成swagger2

守給你的承諾、 提交于 2020-03-01 20:50:50

springboot-swagger2-demo

项目介绍

SpringBoot集成swagger2

使用说明

  • 引入maven依赖
<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>
  • 启用swagger2
在Application类上添加@EnableSwagger2注解 

如:
@EnableSwagger2
@SpringBootApplication
public class SpringbootDemoApplication
  • 在Controller上添加注释信息(非必须)
如:
@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

swagger常用注解

@Api:修饰整个类,描述Controller的作用
@ApiOperation:描述一个类的一个方法,或者说一个接口
@ApiParam:单个参数描述
@ApiModel:用对象来接收参数
@ApiProperty:用对象接收参数时,描述对象的一个字段
@ApiResponse:HTTP响应其中1个描述
@ApiResponses:HTTP响应整体描述
@ApiIgnore:使用该注解忽略这个API
@ApiError :发生错误返回的信息
@ApiImplicitParam:一个请求参数
@ApiImplicitParams:多个请求参数

案例

demo地址:https://gitee.com/mengzhang6/springboot-swagger2-demo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!