SpringBoot框架整合SwaggerUI的示例代碼
整合swagger進(jìn)行模塊測(cè)試
注意事項(xiàng):為方便SpringBoot更好的整合Swagger,需要專門(mén)放置在一個(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用來(lái)簡(jiǎn)化實(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>
在該模塊的啟動(dòng)類上添加ComponentScan注解,指定需要掃描的包。例如:@ComponentScan(basePackages={"com.xsha"})
然后啟動(dòng),訪問(wèn)地址:http://127.0.0.1:8001/swagger-ui.html
統(tǒng)一返回結(jié)果的格式(自定義結(jié)果)
在公共模塊下在創(chuàng)建一個(gè)模塊,如common-utils
創(chuàng)建一個(gè)專門(mén)管理狀態(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中測(cè)試即可
到此這篇關(guān)于SpringBoot框架整合SwaggerUI的文章就介紹到這了,更多相關(guān)SpringBoot整合SwaggerUI內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
新的Java訪問(wèn)mysql數(shù)據(jù)庫(kù)工具類的操作代碼
本文通過(guò)實(shí)例代碼給大家介紹新的Java訪問(wèn)mysql數(shù)據(jù)庫(kù)工具類的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-12-12springcloud引入spring-cloud-starter-openfeign失敗的解決
這篇文章主要介紹了springcloud?引入spring-cloud-starter-openfeign失敗的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03SpringBoot參數(shù)校驗(yàn)之@Valid的使用詳解
這篇文章主要通過(guò)示例為大家詳細(xì)介紹一下介紹了SpringBoot參數(shù)校驗(yàn)中@Valid的使用方法,文中的示例代碼講解詳細(xì),需要的可以參考一下2022-06-06SpringBoot中利用MyBatis進(jìn)行數(shù)據(jù)操作的示例
這篇文章主要介紹了SpringBoot中利用MyBatis進(jìn)行數(shù)據(jù)操作,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09Netty源碼分析NioEventLoop初始化線程選擇器創(chuàng)建
這篇文章主要介紹了Netty源碼分析NioEventLoop初始化線程選擇器創(chuàng)建,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03java簡(jiǎn)易文本分割器實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了java簡(jiǎn)易文本分割器的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07