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

配置
第一步
配置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>第二步
使用注解來進行啟動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地址:端口/項目名/swagger-ui.html#/
ApiInfo apiInfo = new ApiInfoBuilder()
.title("項目名稱") //網站標題
.description("項目名稱swagger RESTful APIs......") //網站描述
.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的注解和方法上的注解,最下方有注解的詳細解釋;
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="模型總表信息,分頁查詢",response=String.class)
@ApiImplicitParams({
@ApiImplicitParam(name = "page", value = "分頁的起始頁", required = true, dataType = "String"),
@ApiImplicitParam(name = "limit", value = "每頁顯示的數(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);
//當前頁
Integer currPage = Integer.parseInt(page.trim());
//每頁的數(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);
}
}這樣呢就已經配置好,下面是訪問效果
訪問 原生的swagger 頁面
http://127.0.0.1:666/swagger-ui.html,可以看到如下效果



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



swagger注解的詳細說明
swagger2使用說明:
@Api:用在類上,說明該類的作用
@ApiOperation:用在方法上,說明方法的作用
@ApiIgnore:使用該注解忽略這個API
@ApiImplicitParams:用在方法上包含一組參數(shù)說明
@ApiImplicitParam:用在@ApiImplicitParams注解中,指定一個請求參數(shù)的各個方面
paramType:參數(shù)放在哪個地方
header-->請求參數(shù)的獲?。篅RequestHeader
query-->請求參數(shù)的獲?。篅RequestParam
path(用于restful接口)-->請求參數(shù)的獲?。篅PathVariable
body(不常用)
form(不常用)
name:參數(shù)名
dataType:參數(shù)類型
required:參數(shù)是否必須傳
value:參數(shù)的意思
defaultValue:參數(shù)的默認值
@ApiResponses:用于表示一組響應
@ApiResponse:用在@ApiResponses中,一般用于表達一個錯誤的響應信息
code:數(shù)字,例如400
message:信息,例如"請求參數(shù)沒填好"
response:拋出異常的類
@ApiModel:描述一個Model的信息(這種一般用在post創(chuàng)建的時候,使用@RequestBody這樣的場景,請求參數(shù)無法使用@ApiImplicitParam注解進行描述的時候)
@ApiModelProperty:描述一個model的屬性總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
在Java中以及Spring環(huán)境下操作Redis的過程
文章介紹了在Java和Spring環(huán)境下操作Redis的基本方法,在Java環(huán)境下,使用Maven創(chuàng)建項目并導入Jedis依賴,通過配置端口轉發(fā)訪問Redis,文章總結了Redis的基本命令和類別,如String、list、hash、set和zset,感興趣的朋友跟隨小編一起看看吧2025-03-03
Java +Tomcat + SpringMVC實現(xiàn)頁面訪問示例解析
這篇文章主要介紹了Java +Tomcat + SpringMVC實現(xiàn)頁面訪問示例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-07-07
Java中的有限狀態(tài)機(設計模式——狀態(tài)模式)
這篇文章主要介紹了Java中的有限狀態(tài)機(設計模式——狀態(tài)模式),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08

