springboot 配置使用swagger2操作
swagger是一個(gè)功能強(qiáng)大的在線API文檔的框架,提供了優(yōu)雅的API在線文檔的查閱和測(cè)試功能。
利用swagger2可以很方便的構(gòu)建RESTful風(fēng)格的API文檔,在springboot中使用也非常方便,主要是在controller前配置添加注解就可以了,詳細(xì)配置過(guò)程如下:
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. 配置類的編寫(xiě)
配置類的編寫(xiě)同樣非常簡(jiǎn)單,可以直接復(fù)制粘貼以下代碼,但是一定要注意做適當(dāng)修改,尤其是設(shè)置basePackage的路徑,一定要根據(jù)實(shí)際情況修改。
新建一個(gè)config文件夾,在此文件夾中新建一個(gè)類
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("簡(jiǎn)單優(yōu)雅的restful風(fēng)格")
.termsOfServiceUrl("https://smileyan.cn")
.version("1.0")
.build();
}
}
不能忘記類前面的@EnableSwagger2 與 @Configurable配置注解。以及后面的@Bean注解。
3. @EnableSwagger2 不能忘了
除了這個(gè)位置需要添加這個(gè)注解,還有springboot的運(yùn)行類(application類)也要添加這個(gè)注釋,否則會(huì)出現(xiàn)錯(cuò)誤。
如圖所示,我的application類名為SwaggerApplication,在這個(gè)類上面添加@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. 編寫(xiě)controller類,添加注解,注意這個(gè)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 = "用戶測(cè)試",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)行,打開(kāi)api文檔http://localhost:8080/swagger-ui.html
效果如下:

可以點(diǎn)開(kāi)user-controller,效果如下:

完成測(cè)試。很簡(jiǎn)單吧。
常用注解
@Api : 修飾整個(gè)類,用于描述Controller類
@ApiOperation:描述類的方法,或者說(shuō)一個(gè)接口
@ApiParam:?jiǎn)蝹€(gè)參數(shù)描述
@ApiModel:用對(duì)象來(lái)接收參數(shù)
@ApiProperty:用對(duì)象接收參數(shù)時(shí),描述對(duì)象的一個(gè)字段
@ApiResponse:HTTP響應(yīng)的一個(gè)描述
@ApiResponses:HTTP響應(yīng)的整體描述
@ApiIgnore:使用該注解,表示Swagger2忽略這個(gè)API
@ApiError:發(fā)生錯(cuò)誤返回的信息
@ApiParamImplicit:一個(gè)請(qǐng)求參數(shù)
@ApiParamsImplicit:多個(gè)請(qǐng)求參數(shù)
以上這篇springboot 配置使用swagger2操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java中申請(qǐng)不定長(zhǎng)度數(shù)組ArrayList的方法
今天小編就為大家分享一篇java中申請(qǐng)不定長(zhǎng)度數(shù)組ArrayList的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
使用JAXBContext 設(shè)置xml節(jié)點(diǎn)屬性
這篇文章主要介紹了使用JAXBContext 設(shè)置xml節(jié)點(diǎn)屬性的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
在Java生產(chǎn)環(huán)境下進(jìn)行性能監(jiān)控與調(diào)優(yōu)的詳細(xì)過(guò)程
在Java生產(chǎn)環(huán)境下進(jìn)行性能監(jiān)控與調(diào)優(yōu)是一個(gè)復(fù)雜但重要的過(guò)程,它涉及到多個(gè)方面,包括代碼分析、JVM監(jiān)控、線程管理、垃圾收集優(yōu)化、內(nèi)存管理、數(shù)據(jù)庫(kù)交互等,下面我將提供一個(gè)詳細(xì)的概述和示例代碼,需要的朋友可以參考下2025-02-02
Java使用bcrypt實(shí)現(xiàn)對(duì)密碼加密效果詳解
bcrypt是一種自帶鹽值(自動(dòng)加鹽)的加密方案。本文將通過(guò)示例為大家詳細(xì)介紹這一對(duì)密碼進(jìn)行加密的算法,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-03-03
spring mvc中的@PathVariable動(dòng)態(tài)參數(shù)詳解
這篇文章主要介紹了spring mvc中的@PathVariable動(dòng)態(tài)參數(shù)詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
struts2+spring+ibatis框架整合實(shí)現(xiàn)增刪改查
這篇文章主要為大家詳細(xì)介紹了struts2+spring+ibatis框架整合實(shí)現(xiàn)增刪改查操作,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
Seata集成Mybatis-Plus解決多數(shù)據(jù)源事務(wù)問(wèn)題
當(dāng)進(jìn)行業(yè)務(wù)操作時(shí),訂單發(fā)生異常 ,進(jìn)行了回滾操作,因?yàn)樵诓煌臄?shù)據(jù)庫(kù)實(shí)例中,余額卻扣除成功,此時(shí)發(fā)現(xiàn)數(shù)據(jù)不一致問(wèn)題,本文給大家介紹Seata集成Mybatis-Plus解決多數(shù)據(jù)源事務(wù)問(wèn)題,感興趣的朋友一起看看吧2023-11-11
Spring?Security實(shí)現(xiàn)HTTP認(rèn)證
本文主要介紹了Spring?Security實(shí)現(xiàn)HTTP認(rèn)證,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧<BR>2022-06-06

