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的一些基本的内容,比如扫描的包等等@Beanpublic 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')// 创建人信息.contact(new Contact('scy', '666', '888'))// 版本号.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 这里是设置是否启动 本地和测试环境为true 正式环境为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内容请搜索好吧啦网以前的文章或继续浏览下面的相关文章希望大家以后多多支持好吧啦网!
相关文章:
1. 《CSS3实战》笔记--渐变设计(一)2. python实现MySQL指定表增量同步数据到clickhouse的脚本3. Springboot+WebSocket实现一对一聊天和公告的示例代码4. 如何在JavaScript中使用localStorage详情5. JavaScript实现移动端带transition动画的轮播效果6. 使用vue打包进行云服务器上传的问题7. 给JavaScript初学者的23条最佳实践8. PHP Ajax跨域问题解决方案代码实例9. 浅析vue cli3 封装Svgicon组件正确姿势(推荐)10. XML基本概念XPath、XSLT与XQuery函数介绍
