手把手教你SpringBoot快速集成Swagger的配置過程
導(dǎo)語(yǔ)
相信大家無(wú)論是做前端還是做后端的,都被接口接口文檔所折磨過,前端抱怨接口文檔和后端給的不一致,后端抱怨寫接口文檔很麻煩,所以Swagger就誕生了。直接配置即可自動(dòng)生成接口文檔,而且提供了高效的API測(cè)試
話不多說(shuō)直接開干
導(dǎo)入SpringBoot集成Swagger所需要的依賴
<!--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 //開啟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)安的開發(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í)體類
@ApiModelProperty( ) //主要用來(lái)標(biāo)注實(shí)體類中的屬性
案例:
@ApiModel("用戶的實(shí)體類")
public class User implements Serializable {
@ApiModelProperty("用戶的id")
private Integer id;
@ApiModelProperty("用戶的姓名")
private String name;
@ApiModelProperty("用戶的年紀(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ān)SpringBoot集成Swagger內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot自定義配置及自定義對(duì)象映射的全流程
這篇文章主要介紹了springboot自定義配置及自定義對(duì)象映射的全流程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
SpringBoot項(xiàng)目刪除Bean或者不加載Bean的問題解決
文章介紹了在Spring Boot項(xiàng)目中如何使用@ComponentScan注解和自定義過濾器實(shí)現(xiàn)不加載某些Bean的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2025-01-01
SpringCloud基于Feign的可編程式接口調(diào)用實(shí)現(xiàn)
本文主要介紹了SpringCloud基于Feign的可編程式接口調(diào)用實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
Java基礎(chǔ)類學(xué)習(xí)之String詳解
這篇文章主要為大家詳細(xì)介紹了Java基礎(chǔ)類中String的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Java有一定的幫助,需要的可以參考一下2022-12-12
SpringCloud服務(wù)接口調(diào)用OpenFeign及使用詳解
這篇文章主要介紹了SpringCloud服務(wù)接口調(diào)用——OpenFeign,在學(xué)習(xí)Ribbon時(shí),服務(wù)間調(diào)用使用的是RestTemplate+Ribbon實(shí)現(xiàn),而Feign在此基礎(chǔ)上繼續(xù)進(jìn)行了封裝,使服務(wù)間調(diào)用變得更加方便,需要的朋友可以參考下2023-04-04
IntelliJ IDEA中如何構(gòu)建Spring Boot的項(xiàng)目
這篇文章主要介紹了IntelliJ IDEA中如何構(gòu)建Spring Boot的項(xiàng)目問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
Java通過Freemarker模板實(shí)現(xiàn)生成Word文件
FreeMarker是一款模板引擎: 即一種基于模板和要改變的數(shù)據(jù), 并用來(lái)生成輸出文本的通用工具。本文將根據(jù)Freemarker模板實(shí)現(xiàn)生成Word文件,需要的可以參考一下2022-09-09
Mybatis-Plus或PageHelper多表分頁(yè)查詢總條數(shù)不對(duì)問題的解決方法
PageHelper 這個(gè)插件用了很多次了,今天使用的時(shí)候才遇到一個(gè)問題,這篇文章主要給大家介紹了關(guān)于Mybatis-Plus或PageHelper多表分頁(yè)查詢總條數(shù)不對(duì)問題的解決方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
Spring加載屬性文件方式(自動(dòng)加載優(yōu)先級(jí)問題)
這篇文章主要介紹了Spring加載屬性文件方式(自動(dòng)加載優(yōu)先級(jí)問題),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
Spring boot中@Conditional和spring boot的自動(dòng)配置實(shí)例詳解
本文通過實(shí)例給大家介紹了Spring boot中@Conditional和spring boot的自動(dòng)配置,需要的朋友可以參考下2018-05-05

