手把手教你SpringBoot快速集成Swagger的配置過(guò)程
導(dǎo)語(yǔ)
相信大家無(wú)論是做前端還是做后端的,都被接口接口文檔所折磨過(guò),前端抱怨接口文檔和后端給的不一致,后端抱怨寫(xiě)接口文檔很麻煩,所以Swagger就誕生了。直接配置即可自動(dòng)生成接口文檔,而且提供了高效的API測(cè)試
話不多說(shuō)直接開(kāi)干
導(dǎo)入SpringBoot集成Swagger所需要的依賴(lài)
<!--web方便測(cè)試--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- swagger2核心包 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <!-- swagger-ui 可視化界面 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>
Swagger可視化界面可分為三個(gè)區(qū)域
Swagger相關(guān)配置
package com.example.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; import java.util.ArrayList; @Configuration @EnableSwagger2 //開(kāi)啟Swagger的使用 public class SwaggerConfig { @Bean //Swagger的使用主要是要將docket對(duì)象傳入IOC容器 public Docket docket(){ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) //關(guān)于文檔的各種信息 .enable(true) //使Swagger生效 .groupName("常安祖") .select()//選擇掃描的接口 .apis(RequestHandlerSelectors.basePackage("com.example.controller"))//指定掃描的接口 .build(); } public ApiInfo apiInfo(){ Contact contact = new Contact("長(zhǎng)安","https://blog.csdn.net/weixin_45647685","719801748@qq.com");//個(gè)人的聯(lián)系方式 return new ApiInfo("長(zhǎng)安的文檔", "長(zhǎng)安的開(kāi)發(fā)文檔", "1.0", "urn:tos",null, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList());//文檔的各種信息 } }
@ApiModel( ) //主要用來(lái)標(biāo)注返回的實(shí)體類(lèi)
@ApiModelProperty( ) //主要用來(lái)標(biāo)注實(shí)體類(lèi)中的屬性
案例:
@ApiModel("用戶(hù)的實(shí)體類(lèi)") public class User implements Serializable { @ApiModelProperty("用戶(hù)的id") private Integer id; @ApiModelProperty("用戶(hù)的姓名") private String name; @ApiModelProperty("用戶(hù)的年紀(jì)") private Integer age; public Integer getId() { return id; } public User(Integer id, String name, Integer age) { this.id = id; this.name = name; this.age = age; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
@ApiModelProperty用來(lái)標(biāo)注API接口
案例:
package com.yangzihao.controller; import com.yangzihao.entity.User; import io.swagger.annotations.ApiModelProperty; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; @RestController public class UserController { @ApiModelProperty("得到一個(gè)User") @GetMapping("/getUser") public User getUser(){ return new User(1,"測(cè)試",18); } }
進(jìn)入Swagger可視化界面
使用Swagger進(jìn)行接口測(cè)試
執(zhí)行
到此這篇關(guān)于手把手教你SpringBoot快速集成Swagger的配置過(guò)程的文章就介紹到這了,更多相關(guān)SpringBoot集成Swagger內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何使用Jackson和JSON Pointer查詢(xún)解析任何JSON節(jié)點(diǎn)
本文介紹了JSON Pointer是字符串表達(dá)式,可以非常方便解析復(fù)雜JSON節(jié)點(diǎn)值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09關(guān)于springboot集成swagger及knife4j的增強(qiáng)問(wèn)題
這篇文章主要介紹了springboot集成swagger以及knife4j的增強(qiáng),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03SpringBoot之bootstrap和application的區(qū)別解讀
這篇文章主要介紹了SpringBoot之bootstrap和application的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03基于Retrofit+Rxjava實(shí)現(xiàn)帶進(jìn)度顯示的下載文件
這篇文章主要為大家詳細(xì)介紹了基于Retrofit+Rxjava實(shí)現(xiàn)帶進(jìn)度顯示的下載文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Java使用抽象工廠模式實(shí)現(xiàn)的肯德基消費(fèi)案例詳解
這篇文章主要介紹了Java使用抽象工廠模式實(shí)現(xiàn)的肯德基消費(fèi)案例,較為詳細(xì)的分析了抽象工廠模式的定義、原理并結(jié)合實(shí)例形式分析了Java使用抽象工廠模式實(shí)現(xiàn)肯德基消費(fèi)案例的步驟與相關(guān)操作技巧,需要的朋友可以參考下2018-05-05springboot 自定義LocaleResolver實(shí)現(xiàn)切換語(yǔ)言
我們?cè)谧鲰?xiàng)目的時(shí)候,往往有很多項(xiàng)目需要根據(jù)用戶(hù)的需要來(lái)切換不同的語(yǔ)言,使用國(guó)際化就可以輕松解決。這篇文章主要介紹了springboot 自定義LocaleResolver切換語(yǔ)言,需要的朋友可以參考下2019-10-10