javascript
Spring Boot和Swagger UI
我已經一年沒有從頭開始開發Spring Web應用程序了,如果我不參加QA自動化工程師的培訓,那么這段時間甚至會更長。 由于這個原因,我開發了一個示例REST應用程序。 除了Swagger,一切對我來說都很熟悉。 因此,我將描述我在Spring Boot和Swagger UI中獲得的新經驗。
前言
首先,我需要提到我已經使用過Spring IO。 是的,這是我第一次將Spring用作流行的Java框架而不是平臺。 真是令人興奮。 與我以前的Spring經驗相比,Spring IO的配置過程和項目設置變得更加輕松快捷。
根據培訓主題,示例Web應用程序需要具有簡單的業務邏輯。 我決定開發一個應用程序,使房東(房地產經紀人)可以管理他們的房地產(公寓)。 因此,應用程序的用戶可以對房東和公寓執行CRUD操作。
現在,當您知道在什么情況下必須使用swagger時,我可以省略關于應用程序和培訓的其余故事,而跳到本文的主要主題-Swagger和Spring Boot集成。
Spring靴+招搖
為了將Swagger插入Web Spring應用程序,您需要向構建文件(Maven或Gradle)添加依賴項。 它在Git官方頁面上非常簡單明了。
之后,您可以添加一個單獨的配置Java類,該類負責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.*");}}配置文件完成后,您可以繼續使用Controllers。 順便說一句,您需要通過Spring Boot Application類將swagger配置放入掃描區域。
@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文檔。 要檢查它,請啟動您的應用程序并轉到http:// localhost:8080 / api-docs
Spring Boot + Swagger用戶界面
很好的JSON格式的API文檔很好,但是對其他團隊成員(例如前端開發人員)沒有太大幫助。 因此,我們必須插入UI。 從官方git repo下載swagger ui。 之后,將其解壓縮并復制dist目錄,并將其粘貼到src / java / resources中的文件夾 / public或/ static或/ resources中。
現在在swagger中重命名dist 。 打開index.html并更改JavaScript代碼,它應如下所示:
$(function () {var url = window.location.search.match(/url=([^&]+)/);if (url && url.length > 1) {url = decodeURIComponent(url[1]);} else {url = "/api-docs";} //rest of code...這就對了。 現在重新啟動應用程序并導航到http:// localhost:8080 / swagger / index.html
您必須看到以下內容:
翻譯自: https://www.javacodegeeks.com/2015/03/spring-boot-swagger-ui.html
總結
以上是生活随笔為你收集整理的Spring Boot和Swagger UI的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 飞机大战安卓源码(飞机大战安卓)
- 下一篇: 从战中反弹:将Git提交信息作为JSON