springboot項(xiàng)目集成swagger-bootstrap-ui全過程
springboot項(xiàng)目集成swagger-bootstrap-ui
Swagger 是一個(gè)規(guī)范和完整的框架,用于生成、描述、調(diào)用和可視化 RESTful 風(fēng)格的 Web 服務(wù)。
總體目標(biāo)是使客戶端和文件系統(tǒng)作為服務(wù)器以同樣的速度來更新。
作用
接口的文檔在線自動(dòng)生成,功能測(cè)試(特別是前后端分離項(xiàng)目,為與前端同學(xué)合作提供了很大的方便)。
本篇內(nèi)容主要記錄swagger配置,頁(yè)面為swagger-bootstrap-ui 頁(yè)面。

配置
第一步
配置pom.xml
<!-- swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
<!-- 引入swagger-bootstrap-ui包 -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.8.5</version>
</dependency>第二步
使用注解來進(jìn)行啟動(dòng)swagger,添加配置類SwaggerConfig
package cn.cnic.zhongtai.system.config;
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.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;
/**
*
* Created by wdd on 2019/11/1
*
**/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
//http://ip地址:端口/項(xiàng)目名/swagger-ui.html#/
ApiInfo apiInfo = new ApiInfoBuilder()
.title("項(xiàng)目名稱") //網(wǎng)站標(biāo)題
.description("項(xiàng)目名稱swagger RESTful APIs......") //網(wǎng)站描述
.version("9.0") //版本
.contact(new Contact("王棟棟","https://blog.csdn.net/Xiaodongge521","wangdongdong0224@163.com")) //聯(lián)系人
.license("The Apache License") //協(xié)議
.licenseUrl("http://www.baidu.com") //協(xié)議url
.build();
return new Docket(DocumentationType.SWAGGER_2) //swagger版本
.pathMapping("/")
.select()
//掃描那些controller
.apis(RequestHandlerSelectors.basePackage("cn.cnic.zhongtai.system.controller"))
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo);
}
}
第三步
配置Controller的注解和方法上的注解,最下方有注解的詳細(xì)解釋;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
* 整體模型控制類
* Created by wdd on 2019/10/16
*
**/
@RestController
@RequestMapping(value = "/genTable")
@Api(value="整體模型控制類",tags = "整體模型控制類",description = "整體模型控制類描述")
public class GenTableController {
@Resource
private GenTableService genTableService;
/**
* 模型總表信息
* @param page
* @param limit
* @return
*/
@RequestMapping(value = "/model_list" , method = RequestMethod.GET)
@ApiOperation(value="模型總表信息",notes="模型總表信息,分頁(yè)查詢",response=String.class)
@ApiImplicitParams({
@ApiImplicitParam(name = "page", value = "分頁(yè)的起始頁(yè)", required = true, dataType = "String"),
@ApiImplicitParam(name = "limit", value = "每頁(yè)顯示的數(shù)量", required = true, dataType = "String")
})
public String modellist( @RequestParam(value="page",defaultValue="1") String page, @RequestParam (value="limit",defaultValue="10") String limit) {
Map<String, Object> rtnMap = new HashMap<String, Object>();
rtnMap.put("code", 500);
//當(dāng)前頁(yè)
Integer currPage = Integer.parseInt(page.trim());
//每頁(yè)的數(shù)量
Integer pageSize = Integer.parseInt(limit.trim());
PageHelper.startPage(currPage, pageSize, true);
List<GenTable> GenTableList = genTableService.selectGenTableList();
PageInfo<GenTable> pageInfo=new PageInfo<>(GenTableList);
if(!GenTableList.isEmpty()) {
rtnMap.put("msg", "success");
rtnMap.put("code", "0");
rtnMap.put("data", GenTableList);
rtnMap.put("count", pageInfo.getTotal());
}else {
rtnMap.put("msg", "error");
rtnMap.put("code", "222222");
}
return JsonUtils.toJsonNoException(rtnMap);
}
}這樣呢就已經(jīng)配置好,下面是訪問效果
訪問 原生的swagger 頁(yè)面
http://127.0.0.1:666/swagger-ui.html,可以看到如下效果



訪問 swagger-bootstrap-ui 的頁(yè)面
http://127.0.0.1:666/doc.html,可以看到如下效果



swagger注解的詳細(xì)說明
swagger2使用說明:
@Api:用在類上,說明該類的作用
@ApiOperation:用在方法上,說明方法的作用
@ApiIgnore:使用該注解忽略這個(gè)API
@ApiImplicitParams:用在方法上包含一組參數(shù)說明
@ApiImplicitParam:用在@ApiImplicitParams注解中,指定一個(gè)請(qǐng)求參數(shù)的各個(gè)方面
paramType:參數(shù)放在哪個(gè)地方
header-->請(qǐng)求參數(shù)的獲?。篅RequestHeader
query-->請(qǐng)求參數(shù)的獲取:@RequestParam
path(用于restful接口)-->請(qǐng)求參數(shù)的獲?。篅PathVariable
body(不常用)
form(不常用)
name:參數(shù)名
dataType:參數(shù)類型
required:參數(shù)是否必須傳
value:參數(shù)的意思
defaultValue:參數(shù)的默認(rèn)值
@ApiResponses:用于表示一組響應(yīng)
@ApiResponse:用在@ApiResponses中,一般用于表達(dá)一個(gè)錯(cuò)誤的響應(yīng)信息
code:數(shù)字,例如400
message:信息,例如"請(qǐng)求參數(shù)沒填好"
response:拋出異常的類
@ApiModel:描述一個(gè)Model的信息(這種一般用在post創(chuàng)建的時(shí)候,使用@RequestBody這樣的場(chǎng)景,請(qǐng)求參數(shù)無法使用@ApiImplicitParam注解進(jìn)行描述的時(shí)候)
@ApiModelProperty:描述一個(gè)model的屬性總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java中for(;;)和while(true)的區(qū)別
這篇文章主要介紹了 Java中for(;;)和while(true)的區(qū)別,文章圍繞for(;;)和while(true)的相關(guān)自來哦展開詳細(xì)內(nèi)容,需要的小伙伴可以參考一下,希望對(duì)大家有所幫助2021-11-11
使用Springboot 打jar包實(shí)現(xiàn)分離依賴lib和配置
這篇文章主要介紹了使用Springboot 打jar包實(shí)現(xiàn)分離依賴lib和配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
Mybatis使用collection標(biāo)簽進(jìn)行樹形結(jié)構(gòu)數(shù)據(jù)查詢時(shí)攜帶外部參數(shù)查詢
這篇文章主要介紹了Mybatis使用collection標(biāo)簽進(jìn)行樹形結(jié)構(gòu)數(shù)據(jù)查詢時(shí)攜帶外部參數(shù)查詢,需要的朋友可以參考下2023-10-10
長(zhǎng)度最小的子數(shù)組題目詳解(Java版)
這篇文章主要給大家介紹了關(guān)于長(zhǎng)度最小的子數(shù)組(Java版)的相關(guān)資料,這到題來自力扣,通過學(xué)習(xí)本文對(duì)大家理解這道題目有很大的幫助,需要的朋友可以參考下2023-12-12
通過java備份恢復(fù)mysql數(shù)據(jù)庫(kù)的實(shí)現(xiàn)代碼
這篇文章主要介紹了如何通過java備份恢復(fù)mysql數(shù)據(jù)庫(kù),其實(shí)一般情況下通過bat或sh就可以,這里主要是介紹了java的實(shí)現(xiàn)思路,喜歡的朋友可以參考下2013-09-09
Java中LinkedList的模擬實(shí)現(xiàn)
本文主要介紹了Java中LinkedList的模擬實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
Spring 校驗(yàn)(validator,JSR-303)簡(jiǎn)單實(shí)現(xiàn)方式
這篇文章主要介紹了Spring 校驗(yàn)(validator,JSR-303)簡(jiǎn)單實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10

