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

歡迎訪問 生活随笔!

生活随笔

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

javascript

Spring Boot和Swagger UI

發(fā)布時(shí)間:2023/12/3 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Boot和Swagger UI 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

我已經(jīng)一年沒有從頭開始開發(fā)Spring Web應(yīng)用程序了,如果我不參加QA自動(dòng)化工程師的培訓(xùn),那么這段時(shí)間甚至?xí)L(zhǎng)。 由于這個(gè)原因,我開發(fā)了一個(gè)示例REST應(yīng)用程序。 除了Swagger,一切對(duì)我來說都很熟悉。 因此,我將描述我在Spring BootSwagger UI中獲得的新經(jīng)驗(yàn)。

前言

首先,我需要提到我已經(jīng)使用過Spring IO。 是的,這是我第一次將Spring用作流行的Java框架而不是平臺(tái)。 真是令人興奮。 與我以前的Spring經(jīng)驗(yàn)相比,Spring IO的配置過程和項(xiàng)目設(shè)置變得更加輕松快捷。

根據(jù)培訓(xùn)主題,示例Web應(yīng)用程序需要具有簡(jiǎn)單的業(yè)務(wù)邏輯。 我決定開發(fā)一個(gè)應(yīng)用程序,使房東(房地產(chǎn)經(jīng)紀(jì)人)可以管理他們的房地產(chǎn)(公寓)。 因此,應(yīng)用程序的用戶可以對(duì)房東和公寓執(zhí)行CRUD操作。

現(xiàn)在,當(dāng)您知道在什么情況下必須使用swagger時(shí),我可以省略關(guān)于應(yīng)用程序和培訓(xùn)的其余故事,而跳到本文的主要主題-Swagger和Spring Boot集成。

Spring靴+招搖

為了將Swagger插入Web Spring應(yīng)用程序,您需要向構(gòu)建文件(Maven或Gradle)添加依賴項(xiàng)。 它在Git官方頁(yè)面上非常簡(jiǎn)單明了。

之后,您可以添加一個(gè)單獨(dú)的配置Java類,該類負(fù)責(zé)Swagger:

import com.mangofactory.swagger.configuration.SpringSwaggerConfig; import com.mangofactory.swagger.models.dto.ApiInfo; import com.mangofactory.swagger.plugin.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.*;@Configuration @EnableSwagger @EnableAutoConfiguration public class SwaggerConfig {private SpringSwaggerConfig springSwaggerConfig;@Autowiredpublic void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig) {this.springSwaggerConfig = springSwaggerConfig;}@Beanpublic SwaggerSpringMvcPlugin customImplementation() {return new SwaggerSpringMvcPlugin(this.springSwaggerConfig)//This info will be used in Swagger. See realisation of ApiInfo for more details..apiInfo(new ApiInfo("SmartMe education API","This app is for education, training purpose. It represents model of landlords and apartments for rent",null,null,null,null))//Here we disable auto generating of responses for REST-endpoints.useDefaultResponseMessages(false)//Here we specify URI patterns which will be included in Swagger docs. Use regex for this purpose..includePatterns("/landlords.*");}}

配置文件完成后,您可以繼續(xù)使用Controllers。 順便說一句,您需要通過Spring Boot Application類將swagger配置放入掃描區(qū)域。

@Api(basePath = "/landlords", value = "Landlords", description = "Operations with Landlords", produces = "application/json") @RestController @RequestMapping(value = "/landlords", produces = MediaType.APPLICATION_JSON_VALUE) public class LandLordController {private static final Logger logger = LoggerFactory.getLogger(LandLordController.class);@Autowiredprivate LandLordService landLordService;@RequestMapping(method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_VALUE)@ResponseStatus(HttpStatus.CREATED)@ApiOperation(value = "Create new Landlord", notes = "Creates new Landlord")@ApiResponses(value = {@ApiResponse(code = 400, message = "Fields are with validation errors"),@ApiResponse(code = 201, message = "") })public LandLord createLandLord(@Valid @RequestBody LandLordDTO landLordDTO) {logger.info("LandLord DTO is: "+landLordDTO);LandLord landLord = new LandLord(landLordDTO);landLordService.create(landLord);return landLord;}//Other endpoints are omitted }

這就是所有需要JSON格式的API文檔。 要檢查它,請(qǐng)啟動(dòng)您的應(yīng)用程序并轉(zhuǎn)到http:// localhost:8080 / api-docs

Spring Boot + Swagger用戶界面

很好的JSON格式的API文檔很好,但是對(duì)其他團(tuán)隊(duì)成員(例如前端開發(fā)人員)沒有太大幫助。 因此,我們必須插入U(xiǎn)I。 從官方git repo下載swagger ui。 之后,將其解壓縮并復(fù)制dist目錄,并將其粘貼到src / java / resources中的文件夾 / public或/ static或/ resources中。

現(xiàn)在在swagger中重命名dist 。 打開index.html并更改JavaScript代碼,它應(yīng)如下所示:

$(function () {var url = window.location.search.match(/url=([^&]+)/);if (url && url.length > 1) {url = decodeURIComponent(url[1]);} else {url = "/api-docs";} //rest of code...

這就對(duì)了。 現(xiàn)在重新啟動(dòng)應(yīng)用程序并導(dǎo)航到http:// localhost:8080 / swagger / index.html

您必須看到以下內(nèi)容:


翻譯自: https://www.javacodegeeks.com/2015/03/spring-boot-swagger-ui.html

總結(jié)

以上是生活随笔為你收集整理的Spring Boot和Swagger UI的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

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