springboot 配置使用swagger2操作
swagger是一個功能強(qiáng)大的在線API文檔的框架,提供了優(yōu)雅的API在線文檔的查閱和測試功能。
利用swagger2可以很方便的構(gòu)建RESTful風(fēng)格的API文檔,在springboot中使用也非常方便,主要是在controller前配置添加注解就可以了,詳細(xì)配置過程如下:
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ù)制粘貼以下代碼,但是一定要注意做適當(dāng)修改,尤其是設(shè)置basePackage的路徑,一定要根據(jù)實(shí)際情況修改。
新建一個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@Configurablepublic 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的運(yùn)行類(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@EnableSwagger2public 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. 運(yùn)行,打開api文檔http://localhost:8080/swagger-ui.html
效果如下:
可以點(diǎn)開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)容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 解決AJAX返回狀態(tài)200沒有調(diào)用success的問題2. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)3. 如何在jsp界面中插入圖片4. 爬取今日頭條Ajax請求5. jsp EL表達(dá)式詳解6. Ajax實(shí)現(xiàn)表格中信息不刷新頁面進(jìn)行更新數(shù)據(jù)7. jsp中sitemesh修改tagRule技術(shù)分享8. asp(vbscript)中自定義函數(shù)的默認(rèn)參數(shù)實(shí)現(xiàn)代碼9. ASP基礎(chǔ)知識VBScript基本元素講解10. JSP servlet實(shí)現(xiàn)文件上傳下載和刪除
