Spring boot集成swagger2生成接口文檔的全過程
一、Swagger介紹
Swagger是一個(gè)規(guī)范和完整的框架,用于生成、描述、調(diào)用和可視化RESTful風(fēng)格的web服務(wù)。目標(biāo)是使客戶端和文件系統(tǒng)作為服務(wù)器以同樣的速度來更新文件的方法,參數(shù)和模型緊密集成到服務(wù)器。這個(gè)解釋簡(jiǎn)單點(diǎn)來講就是說,swagger是一款可以根據(jù)restful風(fēng)格生成的接口開發(fā)文檔,并且支持做測(cè)試的一款中間軟件。
二、使用swagger優(yōu)勢(shì)
1、對(duì)于后端開發(fā)人員來說
- 不用再手寫Wiki接口拼大量參數(shù),避免手寫錯(cuò)誤
- 對(duì)代碼侵入性低,采用全注解的方式,開發(fā)簡(jiǎn)單
- 方法參數(shù)名修改、新增、減少參數(shù)都可以直接生效,不用手動(dòng)維護(hù)
- 缺點(diǎn):增加了開發(fā)成本,寫接口還得再寫一套參數(shù)配置
2、對(duì)前端開發(fā)來說
- 后端只需要定義好接口,會(huì)自動(dòng)生成文檔,接口功能、參數(shù)一目了然
- 聯(lián)調(diào)方便,如果出了問題,直接測(cè)試接口,實(shí)時(shí)檢查參數(shù)和返回值,就可以快速定位是前端還是后端的問題
3、對(duì)于測(cè)試來說
- 但對(duì)于測(cè)試沒有前端界面UI的功能,可以直接用它來測(cè)試接口
- 操作簡(jiǎn)單,不用了解具體代碼就可以操作
三、springboot集成swagger使用
1、新建maven項(xiàng)目(結(jié)構(gòu)如下:)

2、配置pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.dds.sbswagger</groupId>
<artifactId>sb-swagger</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sb-swagger</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3、程序啟動(dòng)類
package com.dds.sbswagger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author dds
*/
@SpringBootApplication
@Slf4j
public class SbSwaggerApplication {
public static void main(String[] args) {
SpringApplication.run(SbSwaggerApplication.class, args);
log.info("\n----------------------------------------------------------\n\t" +
"Application demo is running! Access URLs:\n\t" +
"swagger-ui: \thttp://127.0.0.1:8080/swagger-ui.html\n\t" +
"----------------------------------------------------------");
}
}
4、SwaggerConfig配置類
package com.dds.sbswagger.config;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
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.Collections;
/**
* @author DDS
* @date 2019/9/10 13:55
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.dds.sbswagger.controller"))
//加了ApiOperation注解的類,才生成接口文檔
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfo(
"Spring Boot項(xiàng)目集成Swagger實(shí)例文檔",
"我的微信公眾號(hào):大道七哥,歡迎大家關(guān)注。",
"API V1.0",
"Terms of service",
new Contact("大道七哥", "https://www.cnblogs.com/jstarseven/", "jstarseven@163.com"),
"Apache", "http://www.apache.org/", Collections.emptyList());
}
}
5、實(shí)體類model
package com.dds.sbswagger.model;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* @author DDS
* @date 2019/9/10 13:55
*/
@ApiModel("用戶實(shí)體")
@Data
public class User {
/**
* 用戶Id
*/
@ApiModelProperty("用戶id")
private int id;
/**
* 用戶名
*/
@ApiModelProperty(value = "用戶姓名", example = "zhangdan", required = true)
private String name;
/**
* 用戶地址
*/
@ApiModelProperty(value = "用戶地址", example = "北京市海淀區(qū)", required = true)
private String address;
/**
* 用戶手機(jī)號(hào)
*/
@ApiModelProperty(value = "用戶手機(jī)號(hào)", example = "15689652367", required = true)
private String phone;
/**
* 用戶年齡
*/
@ApiModelProperty(value = "用戶年齡", example = "24", required = true)
private Integer age;
}
6、接口開發(fā)
package com.dds.sbswagger.controller;
import com.dds.sbswagger.model.User;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
/**
* @author DDS
* @date 2019/9/10 13:55
*/
@RestController
@RequestMapping("/user")
@Api(tags = "用戶相關(guān)接口", description = "提供用戶相關(guān)的Rest API")
public class UserController {
@PostMapping("/add")
@ApiOperation(value = "新增用戶接口", notes = "手機(jī)號(hào)、密碼都是必輸項(xiàng),年齡隨邊填,但必須是數(shù)字")
@ApiImplicitParams({
@ApiImplicitParam(name = "name", value = "用戶名稱", required = true, paramType = "form"),
@ApiImplicitParam(name = "address", value = "用戶地址", required = true, paramType = "form"),
@ApiImplicitParam(name = "phone", value = "用戶手機(jī)號(hào)", required = true, paramType = "form"),
@ApiImplicitParam(name = "age", value = "用戶年齡", required = true, paramType = "form", dataType = "Integer")
})
public boolean addUser(@RequestBody User user) {
return false;
}
@ApiOperation("通過id查找用戶接口")
@GetMapping("/find/{id}")
public User findById(@PathVariable("id") int id) {
return new User();
}
@ApiOperation("更新用戶信息接口")
@PutMapping("/update")
@ApiResponses({
@ApiResponse(code = 400, message = "請(qǐng)求參數(shù)沒填好"),
@ApiResponse(code = 404, message = "請(qǐng)求路徑?jīng)]有或頁面跳轉(zhuǎn)路徑不對(duì)"),
@ApiResponse(code = 405, message = "未知錯(cuò)誤")
})
public boolean update(@RequestBody User user) {
return true;
}
@ApiOperation("刪除用戶接口")
@DeleteMapping("/delete/{id}")
public boolean delete(@PathVariable("id") int id) {
return true;
}
}
7、swagger界面顯示

總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Springboot實(shí)現(xiàn)郵箱驗(yàn)證碼注冊(cè)與修改密碼及登錄功能詳解流程
驗(yàn)證碼作為一種自然人的機(jī)器人的判別工具,被廣泛的用于各種防止程序做自動(dòng)化的場(chǎng)景中。傳統(tǒng)的字符型驗(yàn)證安全性已經(jīng)名存實(shí)亡的情況下,各種新型的驗(yàn)證碼如雨后春筍般涌現(xiàn),今天給大家分享一篇SpringBoot實(shí)現(xiàn)滑塊驗(yàn)證碼2022-11-11
java計(jì)算日期相差天數(shù)的4種簡(jiǎn)單方法舉例
最近在工作中遇見一個(gè)小需求,要求計(jì)算兩個(gè)日期之間相差幾天,下面這篇文章主要給大家介紹了關(guān)于java計(jì)算日期相差天數(shù)的4種簡(jiǎn)單方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06
Java Spring WEB應(yīng)用實(shí)例化如何實(shí)現(xiàn)
這篇文章主要介紹了Java Spring WEB應(yīng)用實(shí)例化如何實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12
SrpingDruid數(shù)據(jù)源加密數(shù)據(jù)庫密碼的示例代碼
本篇文章主要介紹了SrpingDruid數(shù)據(jù)源加密數(shù)據(jù)庫密碼的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10
Spring事務(wù)失效場(chǎng)景實(shí)例詳解
實(shí)際項(xiàng)目開發(fā)中,如果涉及到多張表操作時(shí),為了保證業(yè)務(wù)數(shù)據(jù)的一致性,大家一般都會(huì)采用事務(wù)機(jī)制,好多小伙伴可能只是簡(jiǎn)單了解一下,遇到事務(wù)失效的情況,便會(huì)無從下手,下面這篇文章主要給大家介紹了關(guān)于Spring事務(wù)失效場(chǎng)景的相關(guān)資料,需要的朋友可以參考下2022-03-03
基于Java實(shí)現(xiàn)中文分詞系統(tǒng)的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用Java語言實(shí)現(xiàn)一個(gè)簡(jiǎn)易的中文分詞系統(tǒng),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以嘗試一下2022-07-07
Spring中的StopWatch記錄操作時(shí)間代碼實(shí)例
這篇文章主要介紹了Spring中的StopWatch記錄操作時(shí)間代碼實(shí)例,spring-framework提供的一個(gè)StopWatch類可以做類似任務(wù)執(zhí)行時(shí)間控制,也就是封裝了一個(gè)對(duì)開始時(shí)間,結(jié)束時(shí)間記錄操作的Java類,需要的朋友可以參考下2023-11-11
Mybatis?Web中的數(shù)據(jù)庫操作方法舉例詳解
Mybatis是一款優(yōu)秀的持久化框架,用于簡(jiǎn)化JDBC的開發(fā),下面這篇文章主要給大家介紹了關(guān)于Mybatis?Web中數(shù)據(jù)庫操作方法的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-09-09

