• 15、Spring Boot 2.x 集成 Swagger UI


    1.15、Spring Boot 2.x 集成 Swagger UI

    1.15.1 pom文件添加swagger包

    <swagger2.version>2.8.0</swagger2.version>
        ……
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>${swagger2.version}</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>${swagger2.version}</version>
    </dependency>
    

    1.15.2 添加Swagger2Config配置文件

    package com.qm.products.prodmp.web.config;
    
    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.ParameterBuilder;
    import springfox.documentation.builders.PathSelectors;
    import springfox.documentation.schema.ModelRef;
    import springfox.documentation.service.ApiInfo;
    import springfox.documentation.service.Parameter;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @Description swagger ui 配置文件
     * @Author hw
     * @Date 2018/11/21 11:52
     * @Version 1.0
     */
    @Configuration
    @ConditionalOnProperty(prefix = "swagger", value = {"enable"}, havingValue = "true")  //  配置文件配置是否启动swagger配置文件
    @EnableSwagger2 // 启用Swagger2
    public class Swagger2Config {
    
        @Bean
        public Docket createRestApi() {
    
            ParameterBuilder tokenParameter = new ParameterBuilder();
            List<Parameter> params = new ArrayList<Parameter>();
            params.add(tokenParameter.name("Authorization").description("授权Token").modelRef(new ModelRef("string")).parameterType("header").required(false).build());
            return new Docket(DocumentationType.SWAGGER_2)
                    .globalOperationParameters(params)
                    .apiInfo(apiInfo())
                    .select()
                    .paths(PathSelectors.any())
                    .build();
        }
    
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("Spring Boot 2.x RESTful APIs")
                    .version("1.0")
                    .build();
        }
    
    }
    
    

    1.15.3 Application类加入@EnableSwagger2注解开启swagger

    @EnableSwagger2
    @SpringBootApplication
    public class SpringBootRestfulApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringBootRestfulApplication.class, args);
        }
    }
    

    1.15.4 使用。

    • 方法
        @GetMapping("abnormal-pre")
        @ApiImplicitParams({
                @ApiImplicitParam(name = "Authorization", value = "token", required = true, dataType = "string", paramType = "header")
        })
        public BaseRestResponse getAbnormalPre(@RequestParam String startDate, @RequestParam String endDate) {
            List<AbnormalPreDTO> abnormalPreDTOS = onlineService.getAbnormalPre(startDate, endDate);
            return BaseRestResponse.ok(abnormalPreDTOS);
        }
    
       @ApiOperation(value = "模糊搜索本公司用户  用户名/邮箱")
       public BaseRestResponse fuzzySearchUsers(@RequestParam String param) {
       
    
    • class
        @RestController
        @Api(tags = "Admin权限资源管理")
        @RequestMapping("authority")
        public class AuthorityController {
    

    1.15.5 UI

    访问 : http://localhost:8080/swagger-ui.html,就可以看到效果了。

  • 相关阅读:
    PAT 字符串-02 删除字符串中的子串
    带滚动条的文本文件
    PAT IO-04 混合类型数据格式化输入(5)
    PAT IO-03 整数均值
    PAT IO-02 整数四则运算
    linux 之shell
    linux 软件包安装-脚本安装
    Linux 关闭防火墙命令
    linux RPM包管理-yum在线管理
    linux 软件包管理
  • 原文地址:https://www.cnblogs.com/Grand-Jon/p/10006386.html
Copyright © 2020-2023  润新知