SpringBoot整合Swagger框架過程解析
Swagger 是一個規(guī)范和完整的框架,用于生成、描述、調(diào)用和可視化 RESTful 風(fēng)格的 Web 服務(wù)。
總體目標(biāo)是使客戶端和文件系統(tǒng)作為服務(wù)器以同樣的速度來更新。文件的方法、參數(shù)和模型緊密集成到服務(wù)器端的代碼,允許 API 來始終保持同步。Swagger 讓部署管理和使用功能強(qiáng)大的 API 從未如此簡單。
引入maven依賴
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- 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/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
<scope>provided</scope>
</dependency>
創(chuàng)建配置類
package com.example.demo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
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;
/**
* @author yvioo。
*/
@Configuration
@EnableSwagger2 //開啟Swagger2
public class SwaggerConfig {
/**
* 配置Swagger的Docket的bean實(shí)例
* @return
*/
@Bean
public Docket docket(Environment environment) {
//設(shè)置只在開發(fā)中環(huán)境中啟動swagger
Profiles profiles=Profiles.of("dev");
//表示如果現(xiàn)在是dev環(huán)境,則返回true 開啟swagger
boolean flag=environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//是否啟動swagger 默認(rèn)啟動
.enable(flag)
//所在分組
.groupName("yvioo")
.select()
//指定掃描的包路徑
.apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
//指定掃描的請求,這里表示掃描 /hello/ 的請求
//.paths(PathSelectors.ant("/hello/**"))
.build();
}
/**
* 配置ApiInfo信息
* @return
*/
private ApiInfo apiInfo() {
//作者信息
Contact author = new Contact("yvioo", "https://www.cnblogs.com/pxblog/", "111@qq.com");
return new ApiInfo(
"Swagger測試",
"Swagger描述",
"1.0",
"urn:tos",
author,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList()
);
}
}
測試用戶實(shí)體類
User.java
package com.example.demo.entity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@ApiModel("用戶實(shí)體類 User") //增加實(shí)體類接口注釋
@Data //使用Lombok插件自動生成get set方法,這樣才能在swagger中顯示屬性值
public class User {
@ApiModelProperty("用戶ID") //增加字段接口注釋
private Integer id;
@ApiModelProperty("用戶名")
private String username;
}
測試控制器
SwaggerController.java
package com.example.demo.controller;
import com.example.demo.entity.User;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SwaggerController {
@GetMapping("/hello")
public String hello(){
return "hello";
}
/**
* 接口返回值含有實(shí)體類,就會被swagger掃描
*
* @return
*/
@ApiOperation("查詢用戶方法注釋")
@GetMapping(value = "/get/{id}")
public User get(@ApiParam("請求參數(shù)注釋") @PathVariable(value = "id")Integer id){
return new User();
}
}
使用dev環(huán)境 啟動項目后 瀏覽器打開http://localhost:8081/swagger-ui.html#/ 我這里用的端口是8081
顯示效果

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java基礎(chǔ)--自己動手實(shí)現(xiàn)一個LRU
這篇文章主要介紹了運(yùn)用方案如何實(shí)現(xiàn)LUR,文章中通過代碼講解的非常詳細(xì),對大家的工作或?qū)W習(xí)有一定的參考價值,感興趣的朋友可以參考一下2021-08-08
SpringBoot中的CompletableFuture類詳解
這篇文章主要介紹了SpringBoot中的CompletableFuture類詳解,在?Java8中,引入了CompletableFuture類,它提供了一種簡單而強(qiáng)大的方式來執(zhí)行異步任務(wù),今天我們就來詳細(xì)解讀一下這個類,需要的朋友可以參考下2023-07-07
SpringBoot中的maven插件spring-boot-maven-plugin使用
這篇文章主要介紹了SpringBoot中的maven插件spring-boot-maven-plugin使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
Spring Boot中使用Redis做緩存的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于Spring Boot中使用Redis做緩存的相關(guān)資料,文中介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-06-06
詳解全局事務(wù)注解@GlobalTransactional的識別
這篇文章主要為大家介紹了詳解全局事務(wù)注解@GlobalTransactional的識別源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
springboot?@Async?注解如何實(shí)現(xiàn)方法異步
這篇文章主要介紹了springboot?@Async?注解如何實(shí)現(xiàn)方法異步,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
Java連接SAP RFC實(shí)現(xiàn)數(shù)據(jù)抽取的示例詳解
這篇文章主要為大家學(xué)習(xí)介紹了Java如何連接SAP RFC實(shí)現(xiàn)數(shù)據(jù)抽取的功能,文中的示例代碼講解詳細(xì),具有一定的參考價值,需要的可以了解下2023-08-08

