spring boot-2.1.16整合swagger-2.9.2 含yml配置文件的代碼詳解
java代碼
package com.oauth.util; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 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; @Configuration @EnableSwagger2 //是否開啟swagger @ConditionalOnProperty(name = "swagger.enable", havingValue = "true") public class Swagger2 { // swagger2的配置文件,這里可以配置swagger2的一些基本的內(nèi)容,比如掃描的包等等 @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select() // 為當前包路徑 .apis(RequestHandlerSelectors.basePackage("com.oauth.controller")).paths(PathSelectors.any()).build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() // 頁面標題 .title("Swagger2") // 創(chuàng)建人信息 .contact(new Contact("scy", "666", "888")) // 版本號 .version("1.0") // 描述 .description("API 描述").build(); } }
yml文件
server: port: 8587 spring: application: name: auth eureka: instance: prefer-ip-address: true client: service-url: defaultZone: http://localhost:8090/eureka/ swagger: enable: true
swagger:
enable: true 這里是設置是否啟動 本地和測試環(huán)境為true 正式環(huán)境為false
controller
package com.oauth.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @RestController @RequestMapping("api") @Api(value = "測試接口", tags = "IndexController") public class IndexController { @ApiOperation(value = "hello") @GetMapping("hello") public String hello() { return "Hello World"; } @ApiOperation(value = "hello2") @GetMapping("api/hello") public String apiHello() { return "Hello World"; } }
打開swagger頁面 localhost:端口號/swagger-ui.html
如果swagger:
enable: false 這里設置為false
總結
到此這篇關于spring boot-2.1.16整合swagger-2.9.2 含yml配置文件的文章就介紹到這了,更多相關spring boot整合swagger內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
java 中Comparable與Comparator詳解與比較
這篇文章主要介紹了java 中Comparable與Comparator詳解與比較的相關資料,需要的朋友可以參考下2017-04-04java 利用反射機制,獲取實體所有屬性和方法,并對屬性賦值
這篇文章主要介紹了 java 利用反射機制,獲取實體所有屬性和方法,并對屬性賦值的相關資料,需要的朋友可以參考下2017-01-01使用mybatis框架連接mysql數(shù)據(jù)庫的超詳細步驟
MyBatis是目前java項目連接數(shù)據(jù)庫的最流行的orm框架了,下面這篇文章主要給大家介紹了關于使用mybatis框架連接mysql數(shù)據(jù)庫的超詳細步驟,文中通過實例代碼和圖文介紹的非常詳細,需要的朋友可以參考下2023-04-04java多態(tài)實現(xiàn)電子寵物系統(tǒng)
這篇文章主要為大家詳細介紹了java多態(tài)實現(xiàn)電子寵物系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02