springboot添加swagger2组件
生活随笔
收集整理的這篇文章主要介紹了
springboot添加swagger2组件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
swagger2是一個可以構建和調試RESTful API文檔的組件,利用swagger2的注解可以快速的在項目中構建Api文檔,并且提供了測試API的功能
1,引入依賴
<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
@Configuration @EnableSwagger2 public class Swagger2Configration {@Beanpublic Docket createRestApi() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("org.my.security")).paths(PathSelectors.any()).build();}private ApiInfo apiInfo() {return new ApiInfoBuilder().title("Spring Boot添加Swagger2組件").description("Spring Boot添加Swagger2組件").version("1.0").build();}}
@configration 標識這是一個配置類
@EnableSwagger2開啟swagger2
apis表示swagger需要掃描的包PathSelectors.any()表示路徑選擇器匹配所有路徑
apiInfo() swagger頁面上的一些展示信息
3,swagger常用注釋
@Api()?用于類;表示標識這個類是swagger的資源?tags–表示說明?
value–也是說明,可以使用tags替代?
@ApiOperation()?用于方法;表示一個http請求的操作?
value用于方法描述?
notes用于提示內容?
@ApiParam()?用于方法,參數,字段說明;表示對參數的添加元數據(說明或是否必填等)?
name–參數名?
value–參數說明?
required–是否必填
@ApiModel()用于類 ;表示對類進行說明,用于參數用實體類接收?
value–表示對象名?
@ApiModelProperty()用于方法,字段; 表示對model屬性的說明或者數據操作更改?
value–字段說明?
name–重寫屬性名字?
dataType–重寫屬性類型?
required–是否必填?
example–舉例說明?
hidden–隱藏
@ApiImplicitParam()?用于方法?
表示單獨的請求參數
@ApiImplicitParams()?用于方法,包含多個 @ApiImplicitParam?
name–參數ming?
value–參數說明?
dataType–數據類型?
paramType–參數類型?
example–舉例說明
@ApiIgnore 作用于方法上,使用這個注解swagger將忽略這個接口
4,打個樣
@RestController @RequestMapping("user") @Api("用戶信息接口") public class UserController {@GetMapping("/{id}")@ApiOperation(value="查詢用戶", notes="根據id來查詢用戶")@ApiImplicitParam(name = "id", value = "用戶ID")@JsonView(User.UserSimplaView.class)public User getUser(@PathVariable String id){User user = new User();user.setUserName("陳明羽");user.setPassWord("root明羽");return user;}@GetMapping("/userDetail/{id}")@ApiOperation(value="查詢用戶詳情", notes="根據id來查詢用戶詳情信息")@ApiImplicitParam(name = "id", value = "用戶ID")@JsonView(User.UserDetailView.class)public User getUserDetail(@PathVariable String id){User user = new User();user.setUserName("陳明羽");user.setPassWord("root明羽");return user;}}//model對象
@ApiModel(value="用戶對象")
public class User {
@ApiModelProperty(value="用戶名",name="userName")
private String userName;
@ApiModelProperty(value="密碼",name="passWord")
private String passWord;
//get(),set()
}5,swagger文檔效果
訪問 http://localhost:8080/swagger-ui.html
點擊單個接口
?6,測試接口
以上圖/user/userDetail接口為例
在Parameters那塊對應字段的value給填上你想測試的值,然后點 try in out!
轉載于:https://www.cnblogs.com/cmyxn/p/8028134.html
總結
以上是生活随笔為你收集整理的springboot添加swagger2组件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python3 CookBook | 数
- 下一篇: POJ1741