springboot 配置使用swagger2操作
swagger是一個功能強大的在線API文檔的框架,提供了優(yōu)雅的API在線文檔的查閱和測試功能。
利用swagger2可以很方便的構(gòu)建RESTful風(fēng)格的API文檔,在springboot中使用也非常方便,主要是在controller前配置添加注解就可以了,詳細配置過程如下:
1. maven依賴包
使用目前最新版本為例,pom.xml添加的代碼如下
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency>
2. 配置類的編寫
配置類的編寫同樣非常簡單,可以直接復(fù)制粘貼以下代碼,但是一定要注意做適當修改,尤其是設(shè)置basePackage的路徑,一定要根據(jù)實際情況修改。
新建一個config文件夾,在此文件夾中新建一個類
package cn.smileyan.swagger.config; import org.springframework.beans.factory.annotation.Configurable; import org.springframework.context.annotation.Bean; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @EnableSwagger2 @Configurable public class Swagger2 { /** * 特別要注意.apis(RequestHandlerSelectors.basePackage("cn.smileyan.swagger.controller")) * 此中的cn.smileyan.swagger.controller一定要修改為自己controller包。 * @return */ @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("cn.smileyan.swagger.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder().title("springboot使用swagger例子") .description("簡單優(yōu)雅的restful風(fēng)格") .termsOfServiceUrl("https://smileyan.cn") .version("1.0") .build(); } }
不能忘記類前面的@EnableSwagger2 與 @Configurable配置注解。以及后面的@Bean注解。
3. @EnableSwagger2 不能忘了
除了這個位置需要添加這個注解,還有springboot的運行類(application類)也要添加這個注釋,否則會出現(xiàn)錯誤。
如圖所示,我的application類名為SwaggerApplication,在這個類上面添加@EnableSwagger2
package cn.smileyan.swagger; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import springfox.documentation.swagger2.annotations.EnableSwagger2; @SpringBootApplication @EnableSwagger2 public class SwaggerApplication { public static void main(String[] args) { SpringApplication.run(SwaggerApplication.class, args); } }
4. 編寫controller類,添加注解,注意這個controller路徑與上面配置類的路徑要保持一致。
package cn.smileyan.swagger.controller; import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; import java.util.Map; @RestController @RequestMapping("/user") public class UserController { @ApiOperation(value = "用戶測試",notes = "貴賓用戶") @RequestMapping(value = "",method = RequestMethod.GET) private Map<String,String> getUser() { Map<String,String> map = new HashMap<>(1); map.put("result","success"); return map; } }
5. 運行,打開api文檔http://localhost:8080/swagger-ui.html
效果如下:
可以點開user-controller,效果如下:
完成測試。很簡單吧。
常用注解
@Api : 修飾整個類,用于描述Controller類
@ApiOperation:描述類的方法,或者說一個接口
@ApiParam:單個參數(shù)描述
@ApiModel:用對象來接收參數(shù)
@ApiProperty:用對象接收參數(shù)時,描述對象的一個字段
@ApiResponse:HTTP響應(yīng)的一個描述
@ApiResponses:HTTP響應(yīng)的整體描述
@ApiIgnore:使用該注解,表示Swagger2忽略這個API
@ApiError:發(fā)生錯誤返回的信息
@ApiParamImplicit:一個請求參數(shù)
@ApiParamsImplicit:多個請求參數(shù)
以上這篇springboot 配置使用swagger2操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于Spring Mvc實現(xiàn)的Excel文件上傳下載示例
本篇文章主要介紹了基于Spring Mvc實現(xiàn)的Excel文件上傳下載示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02Mybatis使用foreach批量更新數(shù)據(jù)報無效字符錯誤問題
這篇文章主要介紹了Mybatis使用foreach批量更新數(shù)據(jù)報無效字符錯誤問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08基于idea 的 Java中的get/set方法之優(yōu)雅的寫法
這篇文章主要介紹了基于idea 的 Java中的get/set方法之優(yōu)雅的寫法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01