springboot項目集成swagger-bootstrap-ui全過程
springboot項目集成swagger-bootstrap-ui
Swagger 是一個規(guī)范和完整的框架,用于生成、描述、調(diào)用和可視化 RESTful 風格的 Web 服務(wù)。
總體目標是使客戶端和文件系統(tǒng)作為服務(wù)器以同樣的速度來更新。
作用
接口的文檔在線自動生成,功能測試(特別是前后端分離項目,為與前端同學合作提供了很大的方便)。
本篇內(nèi)容主要記錄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("項目名稱") //網(wǎng)站標題 .description("項目名稱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的注解和方法上的注解,最下方有注解的詳細解釋;
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); } }
這樣呢就已經(jīng)配置好,下面是訪問效果
訪問 原生的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:用于表示一組響應(yīng) @ApiResponse:用在@ApiResponses中,一般用于表達一個錯誤的響應(yīng)信息 code:數(shù)字,例如400 message:信息,例如"請求參數(shù)沒填好" response:拋出異常的類 @ApiModel:描述一個Model的信息(這種一般用在post創(chuàng)建的時候,使用@RequestBody這樣的場景,請求參數(shù)無法使用@ApiImplicitParam注解進行描述的時候) @ApiModelProperty:描述一個model的屬性
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java中for(;;)和while(true)的區(qū)別
這篇文章主要介紹了 Java中for(;;)和while(true)的區(qū)別,文章圍繞for(;;)和while(true)的相關(guān)自來哦展開詳細內(nèi)容,需要的小伙伴可以參考一下,希望對大家有所幫助2021-11-11使用Springboot 打jar包實現(xiàn)分離依賴lib和配置
這篇文章主要介紹了使用Springboot 打jar包實現(xiàn)分離依賴lib和配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02Mybatis使用collection標簽進行樹形結(jié)構(gòu)數(shù)據(jù)查詢時攜帶外部參數(shù)查詢
這篇文章主要介紹了Mybatis使用collection標簽進行樹形結(jié)構(gòu)數(shù)據(jù)查詢時攜帶外部參數(shù)查詢,需要的朋友可以參考下2023-10-10通過java備份恢復mysql數(shù)據(jù)庫的實現(xiàn)代碼
這篇文章主要介紹了如何通過java備份恢復mysql數(shù)據(jù)庫,其實一般情況下通過bat或sh就可以,這里主要是介紹了java的實現(xiàn)思路,喜歡的朋友可以參考下2013-09-09Spring 校驗(validator,JSR-303)簡單實現(xiàn)方式
這篇文章主要介紹了Spring 校驗(validator,JSR-303)簡單實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10