SpringBoot框架整合SwaggerUI的示例代碼
整合swagger進(jìn)行模塊測試
注意事項(xiàng):為方便SpringBoot更好的整合Swagger,需要專門放置在一個(gè)模塊中(maven子工程)
創(chuàng)建公共模塊,整合swagger,為了所有模塊進(jìn)行使用
common/pom.xml,導(dǎo)入相關(guān)的依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>provided </scope>
</dependency>
<!--mybatis-plus-->
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<!--lombok用來簡化實(shí)體類:需要安裝lombok插件-->
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<!--swagger-->
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<artifactId>springfox-swagger-ui</artifactId>
<!-- redis -->
<artifactId>spring-boot-starter-data-redis</artifactId>
<!-- spring2.X集成redis所需common-pool2
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.0</version>
</dependency>-->
</dependencies>在公共模塊下在創(chuàng)建一個(gè)模塊,如service_base
在該模塊下創(chuàng)建配置類(需要遵循SpringBoot規(guī)范,該代碼固定)
package com.xsha.servicebase;
import com.google.common.base.Predicates;
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.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;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket webApiConfig(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("webApi")
.apiInfo(webApiInfo())
.select()
.paths(Predicates.not(PathSelectors.regex("/admin/.*")))
.paths(Predicates.not(PathSelectors.regex("/error.*")))
.build();
}
private ApiInfo webApiInfo(){
return new ApiInfoBuilder()
.title("網(wǎng)站標(biāo)題")
.description("接口文檔的描述信息")
.version("1.0")
.contact(new Contact("java", "http://www.baidu.com", "1234567890@qq.com"))
}使用方式
在其他模塊(最好是最外層的)的pom.xml引入上面的模塊即可
<dependency>
<groupId>com.xsha</groupId>
<artifactId>service_base</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>在該模塊的啟動類上添加ComponentScan注解,指定需要掃描的包。例如:@ComponentScan(basePackages={"com.xsha"})
然后啟動,訪問地址:http://127.0.0.1:8001/swagger-ui.html
統(tǒng)一返回結(jié)果的格式(自定義結(jié)果)
在公共模塊下在創(chuàng)建一個(gè)模塊,如common-utils
創(chuàng)建一個(gè)專門管理狀態(tài)碼的接口
public interface ResultCode {
//定義兩個(gè)狀態(tài)碼
public static int SUCCESS = 20000;
public static int ERROR = 40000;
}定義返回格式(較為固定)
package com.xsha.commonutils;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.HashMap;
import java.util.Map;
// 統(tǒng)一返回結(jié)果類
@Data
public class R {
@ApiModelProperty(value = "是否成功")
private Boolean success;
@ApiModelProperty(value = "返回碼")
private Integer code;
@ApiModelProperty(value = "返回消息")
private String message;
@ApiModelProperty(value = "返回?cái)?shù)據(jù)")
private Map<String, Object> data = new HashMap<String, Object>();
// 把構(gòu)造方法定為私有
private R() {}
// 成功靜態(tài)方法
public static R ok() {
R r = new R();
r.setSuccess(true);
r.setCode(ResultCode.SUCCESS);
r.setMessage("成功");
return r;
}
// 失敗靜態(tài)方法
public static R error() {
r.setSuccess(false);
r.setCode(ResultCode.ERROR);
r.setMessage("失敗");
public R success(Boolean success){
this.setSuccess(success);
return this;
public R message(String message){
this.setMessage(message);
public R code(Integer code){
this.setCode(code);
public R data(String key, Object value){
this.data.put(key, value);
public R data(Map<String, Object> map){
this.setData(map);
}使用方式
在其他模塊(最好是最外層的)的pom.xml引入上面的模塊即可
<dependency>
<groupId>com.xsha</groupId>
<artifactId>common_utils</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>每次返回的結(jié)果的類型必須是自定義的“返回格式”類類型
// please use rest style
// 1.select all teachers data
@ApiOperation(value = "所有數(shù)據(jù)列表")
@GetMapping("findAll")
public R findAllTeachers() {
List<EduTeacher> teachers = teacherService.list(null);
return R.ok().data("results", teachers);
}
// request path mast have variable id
// 2.logically delete teacher
@ApiOperation(value = "邏輯刪除數(shù)據(jù)")
@DeleteMapping("{id}")
public R logicDeleteTeacher(@ApiParam(name="id", value="講師ID", required = true) @PathVariable String id) {
boolean flag = teacherService.removeById(id);
return flag? R.ok(): R.error();
}最后在swagger中測試即可
到此這篇關(guān)于SpringBoot框架整合SwaggerUI的文章就介紹到這了,更多相關(guān)SpringBoot整合SwaggerUI內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot中的響應(yīng)式web應(yīng)用詳解
這篇文章主要介紹了SpringBoot中的響應(yīng)式web應(yīng)用詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
Java基于Servlet和JSP實(shí)現(xiàn)登錄功能
在 Web 開發(fā)中,用戶登錄功能是非常常見的模塊之一,本文將通過使用 Java Servlet 和 JSP 實(shí)現(xiàn)一個(gè)簡單的用戶登錄功能,展示如何創(chuàng)建登錄頁面、處理用戶登錄請求,并使用數(shù)據(jù)庫驗(yàn)證用戶信息,需要的朋友可以參考下2024-11-11
SpringBoot實(shí)現(xiàn)數(shù)據(jù)庫讀寫分離的3種方法小結(jié)
為了提高系統(tǒng)的讀寫性能和可用性,讀寫分離是一種經(jīng)典的數(shù)據(jù)庫架構(gòu)模式,在SpringBoot應(yīng)用中,有多種方式可以實(shí)現(xiàn)數(shù)據(jù)庫讀寫分離,本文將介紹三種主實(shí)現(xiàn)方案,大家可以根據(jù)需要自行選擇2025-04-04
Java JDK動態(tài)代理(AOP)用法及實(shí)現(xiàn)原理詳解
在本篇文章了小編給大家整理的是一篇關(guān)于Java JDK動態(tài)代理(AOP)用法及實(shí)現(xiàn)原理詳解內(nèi)容,有需要的朋友們可以參考學(xué)習(xí)下。2020-10-10
Java使用Graphics2D實(shí)現(xiàn)字符串文本自動換行
這篇文章主要為大家詳細(xì)介紹了Java如何使用Graphics2D實(shí)現(xiàn)字符串文本自動換行,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-04-04
性能爆棚的實(shí)體轉(zhuǎn)換復(fù)制工具M(jìn)apStruct使用詳解
這篇文章主要為大家介紹了性能爆棚的實(shí)體轉(zhuǎn)換復(fù)制工具M(jìn)apStruct使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03

