SpringBoot集成swagger2

springboot-swagger2-demo

項目介紹

SpringBoot集成swagger2html

使用說明

  • 引入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 git

相關文章
相關標籤/搜索