• springboot 集成swagger 实现不同环境是否开启


    了解到swagger 就记录下用法

    rest.swaggerEnabled 配置文件设置为true开启swagger,false或者不配置,不开启swagger

    pom.xml

    <dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.8.0</version>
    </dependency>
    <dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.8.0</version>
    </dependency>
    Swagger2.java
    package com.zjjw.manager.web.config;

    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
    import org.springframework.beans.factory.annotation.Value;
    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.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;

    /**
    * Swagger的配置信息
    *
    * @author tzq
    * @date 2019-03-18 16:11
    */
    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {

    @Value("${rest.swaggerEnabled:false}")
    private Boolean restConfig ;

    @Bean
    public Docket docket() {
    return new Docket(DocumentationType.SWAGGER_2)
    .enable(restConfig)
    .apiInfo(apiInfo())
    .select()
    .apis(RequestHandlerSelectors.basePackage("com.zjjw.manager.web.controller"))
    .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
    .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
    .paths(PathSelectors.any())
    .build();
    }

    private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
    .title("Cloud-Console API")
    .description("检务后台管理API")
    .version("1.0.0")
    .build();
    }
    }
    Controller注解
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
     
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiImplicitParam;
    import io.swagger.annotations.ApiImplicitParams;
    import io.swagger.annotations.ApiOperation;
     
    @RestController  
    @RequestMapping("test") 
    @Api(value="测试接口Controller")
    public class TestController {
     
    	@ApiOperation(value="测试用接口", notes="测试用接口" ,httpMethod="POST")
    	@ApiImplicitParams({
    		@ApiImplicitParam(name="name", value="用户姓名", dataType = "String", required=true, paramType="form"),
    		@ApiImplicitParam(name="id", value="id", dataType = "int", required=false, paramType="form")
    	})
        @RequestMapping("word")  
        public String HelloWord(String name,Integer id) {  
            return "Hello Word";  
        }
    	
    	
    

      

    访问路径:ip+端口/swagger-ui.html

    swagger  个人感觉,适合快速开发,减少些接口文档的时间,

  • 相关阅读:
    web接口文档apidoc的使用
    python 文件重命名
    sort()排序
    JavaScript自定义事件和触发(createEvent, dispatchEvent)
    Sql 2016 安装到sql_shared_mrconfigaction-install-confignonrc-cpu64卡住不动,是什么原因呢?
    基础提供程序在Open上失败
    win10 Hyper-v 虚拟机监控程序灰色 尝试检索虚拟交换机列表时出错【转载】
    相对路径获取文件名
    省市区三级联动
    反射==>不明确的匹配
  • 原文地址:https://www.cnblogs.com/mytzq/p/9969770.html
Copyright © 2020-2023  润新知