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)容,比如掃描的包等等@Beanpublic Docket createRestApi() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()// 為當(dāng)前包路徑.apis(RequestHandlerSelectors.basePackage('com.oauth.controller')).paths(PathSelectors.any()).build();}private ApiInfo apiInfo() {return new ApiInfoBuilder()// 頁面標(biāo)題.title('Swagger2')// 創(chuàng)建人信息.contact(new Contact('scy', '666', '888'))// 版本號(hào).version('1.0')// 描述.description('API 描述').build();}}
yml文件
server: port: 8587spring: application: name: auth eureka: instance: prefer-ip-address: true client: service-url: defaultZone: http://localhost:8090/eureka/ swagger: enable: true
swagger:enable: true 這里是設(shè)置是否啟動(dòng) 本地和測(cè)試環(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 = '測(cè)試接口', 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:端口號(hào)/swagger-ui.html
如果swagger:enable: false 這里設(shè)置為false
總結(jié)
到此這篇關(guān)于spring boot-2.1.16整合swagger-2.9.2 含yml配置文件的文章就介紹到這了,更多相關(guān)spring boot整合swagger內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. idea設(shè)置自動(dòng)導(dǎo)入依賴的方法步驟2. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)3. css代碼優(yōu)化的12個(gè)技巧4. 利用ajax+php實(shí)現(xiàn)商品價(jià)格計(jì)算5. 教你如何寫出可維護(hù)的JS代碼6. IDEA版最新MyBatis程序配置教程詳解7. 使用Python和百度語音識(shí)別生成視頻字幕的實(shí)現(xiàn)8. idea不能自動(dòng)補(bǔ)全yml配置文件的原因分析9. phpstudy apache開啟ssi使用詳解10. .NET SkiaSharp 生成二維碼驗(yàn)證碼及指定區(qū)域截取方法實(shí)現(xiàn)
