日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

后端:SpringBoot集成Swagger-Bootstrap-UI,界面美观!

發(fā)布時(shí)間:2023/12/10 javascript 53 豆豆
生活随笔 收集整理的這篇文章主要介紹了 后端:SpringBoot集成Swagger-Bootstrap-UI,界面美观! 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

SpringBoot集成Swagger-Bootstrap-UI,界面美觀。下面給大家介紹一下!

該開(kāi)源項(xiàng)目GitHub地址:

https://github.com/xiaoymin/Swagger-Bootstrap-UI

該開(kāi)源項(xiàng)目中文文檔地址:

https://doc.xiaominfo.com/

1

添加Maven依賴

<dependency><groupId>io.springfoxgroupId><artifactId>springfox-swagger2artifactId><version>2.9.2version> dependency> <dependency><groupId>com.github.xiaoymingroupId><artifactId>swagger-bootstrap-uiartifactId><version>1.9.6version> dependency>

2

添加配置類(lèi)

package?com.blog.tutorial.config; import?com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI; 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.spi.DocumentationType; import?springfox.documentation.spring.web.plugins.Docket; import?springfox.documentation.swagger2.annotations.EnableSwagger2; /***?@description:*?@author:?youcong*?@time:?2020/11/14?15:46*/@Configuration @EnableSwagger2 @EnableSwaggerBootstrapUI public?class?SwaggerConfiguration?{@Beanpublic?Docket?createRestApi()?{return?new?Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.blog.tutorial.controller")).paths(PathSelectors.any()).build();}private?ApiInfo?apiInfo()?{return?new?ApiInfoBuilder().title("swagger-bootstrap-ui?RESTful?APIs").description("swagger-bootstrap-ui").termsOfServiceUrl("http://localhost:5050/").contact("developer@mail.com").version("1.0").build();} }

3

啟動(dòng)項(xiàng)目

啟動(dòng)項(xiàng)目,不報(bào)錯(cuò),然后訪問(wèn)地址:


http://ip:port/doc.html 即可

效果圖,如下:

測(cè)試接口,效果圖如下:


調(diào)式相當(dāng)于用PostMan測(cè)試接口。

4

常用注解

和swagger一樣,swagger用的注解,swagger-bootstrap-ui仍能用。


不過(guò)結(jié)合我的開(kāi)發(fā)經(jīng)驗(yàn)來(lái)看,最常用的也就兩個(gè),@Api和@ApiOperation。


@Api的效果,如圖:

@ApiOperation的效果,如圖:


由此,我們很容易就看出來(lái),它們的含義是什么,一個(gè)是接口分類(lèi)說(shuō)明,一個(gè)是接口方法說(shuō)明。

至于這里不用swagger的參數(shù)注解,主要原因是不想加太多的注解從而增加代碼的數(shù)量,造成太多冗余。

例子中的Controller代碼:

package?com.blog.tutorial.controller; import?com.blog.tutorial.entity.Users; import?com.blog.tutorial.service.UsersService; import?io.swagger.annotations.Api; import?io.swagger.annotations.ApiOperation; import?org.springframework.beans.factory.annotation.Autowired; import?org.springframework.web.bind.annotation.GetMapping; import?org.springframework.web.bind.annotation.RequestMapping; import?org.springframework.web.bind.annotation.RestController; import?java.util.List; /***?@description:*?@author:?youcong*?@time:?2020/11/14?13:27*/@RestController @RequestMapping("/user") @Api(tags?=?{"用戶管理"},?description?=?"用戶管理") public?class?UserController?{@Autowiredprivate?UsersService?usersService;@GetMapping("/list")@ApiOperation(value?=?"用戶列表")public?List?list()?{return?usersService.list();} }

5

其它

關(guān)于swagger整合系列,可以參考如下:

https://www.cnblogs.com/youcong/p/9011302.html

關(guān)于swagger-bootstrap整合系列,可以參考:

https://www.cnblogs.com/youcong/p/9196157.html

https://www.cnblogs.com/youcong/p/10786371.html

6

可能遇到的問(wèn)題

1.訪問(wèn)不到接口文檔界面白版

一般是被攔截了(shiro或springsecurity機(jī)制)或者是配置錯(cuò)誤。

2.訪問(wèn)接口文檔界面出來(lái)了,但掃描不到接口

主要是配置類(lèi)的緣故,配置類(lèi)有個(gè)包掃描,必須配置為controller路徑。


如圖所示:

如果還有其它問(wèn)題,可以去官方文檔上找,官方文檔有一個(gè)常規(guī)問(wèn)題列表和解決方案,如圖所示:

如果問(wèn)題非常奇葩的話,實(shí)在解決不了(在參考官方文檔說(shuō)明和搜索的前提下,仍解決不了,把問(wèn)題詳細(xì)描述和關(guān)鍵性代碼提到該開(kāi)源項(xiàng)目的issue上,向創(chuàng)造者求助)。

IT技術(shù)分享社區(qū)

個(gè)人博客網(wǎng)站:https://programmerblog.xyz

文章推薦程序員效率:畫(huà)流程圖常用的工具程序員效率:整理常用的在線筆記軟件遠(yuǎn)程辦公:常用的遠(yuǎn)程協(xié)助軟件,你都知道嗎?51單片機(jī)程序下載、ISP及串口基礎(chǔ)知識(shí)硬件:斷路器、接觸器、繼電器基礎(chǔ)知識(shí)

總結(jié)

以上是生活随笔為你收集整理的后端:SpringBoot集成Swagger-Bootstrap-UI,界面美观!的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。