Spring Boot 項(xiàng)目中使用Swagger2的示例
本文介紹了Spring Boot 項(xiàng)目中使用Swagger2的示例,分享給大家,具體如下:
添加Swagger2依賴
在pom.xml中加入Swagger2的依賴
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.2.2</version> </dependency>
創(chuàng)建Swagger2配置類(lèi)
在Application.java同級(jí)創(chuàng)建Swagger2的配置類(lèi)Swagger2。
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; 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; @Configuration @EnableSwagger2 public class Swagger2 { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("你自己的外部接口包名稱(chēng)")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("詞網(wǎng)Neo4j RESTful APIs") .description("The Neo4j RESTful APIs description/") .termsOfServiceUrl("") .contact("李慶海") .version("5.0") .build(); } }
添加文檔內(nèi)容
在完成了上述配置后,其實(shí)已經(jīng)可以生產(chǎn)文檔內(nèi)容,但是這樣的文檔主要針對(duì)請(qǐng)求本身,而描述主要來(lái)源于函數(shù)等命名產(chǎn)生,對(duì)用戶并不友好,我們通常需要自己增加一些說(shuō)明來(lái)豐富文檔內(nèi)容。
import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; /** * 系統(tǒng)用戶Controller * * @author 李慶海 * */ @Api(value = "系統(tǒng)用戶接口", tags = "系統(tǒng)管理") @RestController @RequestMapping("/v3/edu/users") public class UserController { @Autowired private UserService userService; /** * 添加用戶,注冊(cè) * * @param loginName * 登錄賬號(hào) * @param userName * 用戶名稱(chēng) * @param password * 登錄密碼 * @param roleId * 用戶角色 * @return * @throws ResourceExistsException */ @ApiOperation(value = "添加用戶") @PostMapping("/") public JsonResult create( @ApiParam(name = "loginName", value = "登錄賬號(hào)", required = true) @RequestParam(required = true) @RequestBody String loginName, @ApiParam(name = "userName", value = "用戶名稱(chēng)", required = true) @RequestParam(required = true) @RequestBody String userName, @ApiParam(name = "password", value = "登錄密碼", required = true) @RequestParam(required = true) @RequestBody String password, @ApiParam(name = "roleId", value = "用戶角色編號(hào)", required = true) @RequestParam(required = true) @RequestBody String roleId) throws ResourceExistsException { boolean exists = this.userService.exists(loginName); if (exists) { throw new ResourceExistsException(loginName); } User user = userService.create(loginName, password, userName, roleId); return new JsonResult(user); } }
查看API
啟動(dòng)Spring Boot程序,訪問(wèn):http://localhost:8080/swagger-ui.html
API文檔訪問(wèn)與調(diào)試
Swagger除了查看接口功能外,還提供了調(diào)試測(cè)試功能,我們可以點(diǎn)擊上圖中右側(cè)的Model Schema(黃色區(qū)域:它指明了數(shù)據(jù)結(jié)構(gòu)),此時(shí)Value中就有了user對(duì)象的模板,我們只需要稍適修改,點(diǎn)擊下方Try it out!按鈕,即可完成了一次請(qǐng)求調(diào)用!可以通過(guò)幾個(gè)GET請(qǐng)求來(lái)驗(yàn)證之前的POST請(qǐng)求是否正確。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java+MySQL實(shí)現(xiàn)設(shè)計(jì)優(yōu)惠券系統(tǒng)
這篇文章主要介紹了Java+MySQL實(shí)現(xiàn)設(shè)計(jì)優(yōu)惠券系統(tǒng),文章基于Java與MySQL的相關(guān)資料展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-05-05springboot如何通過(guò)session實(shí)現(xiàn)單點(diǎn)登入詳解
單點(diǎn)登錄(SSO)的定義是在多個(gè)應(yīng)用系統(tǒng)中,用戶只需要登錄一次就可以訪問(wèn)所有相互信任的應(yīng)用系統(tǒng),下面這篇文章主要給大家介紹了關(guān)于springboot如何通過(guò)session實(shí)現(xiàn)單點(diǎn)登入的相關(guān)資料,需要的朋友可以參考下2021-12-12spring @Cacheable擴(kuò)展實(shí)現(xiàn)緩存自動(dòng)過(guò)期時(shí)間及自動(dòng)刷新功能
用過(guò)spring cache的朋友應(yīng)該會(huì)知道,Spring Cache默認(rèn)是不支持在@Cacheable上添加過(guò)期時(shí)間的,雖然可以通過(guò)配置緩存容器時(shí)統(tǒng)一指定,本文主要介紹了如何基于spring @Cacheable擴(kuò)展實(shí)現(xiàn)緩存自動(dòng)過(guò)期時(shí)間以及緩存即將到期自動(dòng)刷新,2024-02-02支付寶二面:使用?try-catch?捕獲異常會(huì)影響性能嗎(推薦)
這篇文章主要介紹了支付寶二面:使用?try-catch?捕獲異常會(huì)影響性能嗎,需要注意的是,JVM?中?異常處理的catch語(yǔ)句不再由字節(jié)碼指令來(lái)實(shí)現(xiàn)(很早之前通過(guò)?jsr和?ret指令來(lái)完成,需要的朋友可以參考下2023-03-03MyBatis多對(duì)多關(guān)聯(lián)映射創(chuàng)建示例
這篇文章主要為大家介紹了MyBatis多對(duì)多關(guān)聯(lián)映射的創(chuàng)建示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06Spring Security CsrfFilter過(guò)濾器用法實(shí)例
這篇文章主要介紹了Spring Security CsrfFilter過(guò)濾器用法實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11關(guān)于 Java 的數(shù)據(jù)結(jié)構(gòu)鏈表
這篇文章主要介紹了關(guān)于 Java 的數(shù)據(jù)結(jié)構(gòu)鏈表的相關(guān)資料,需要的朋友可以參考下面文章內(nèi)容2021-09-09