swagger2 设置全局参数

一、Docket

.globalOperationParameters(getGlobalOperationParameters())

 @Bean
    public Docket createRestApi() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
//                .pathMapping("/")
                .globalOperationParameters(getGlobalOperationParameters())
                .select()
                // 指定当前包路径,这里就添加了两个包,注意方法变成了basePackage,中间加上成员变量splitor
                .apis(RequestHandlerSelectors.basePackage("com.example.lwp.swagger2.swagger2authdemo.controller"))
                .paths(PathSelectors.any())
                .build()
                ;
        return docket;
    }

二、创建全局参数

private List getGlobalOperationParameters() {
        List pars = new ArrayList<>();
        ParameterBuilder parameterBuilder = new ParameterBuilder();
        // header query cookie
        parameterBuilder.name("X-Auth-Token").description("token").modelRef(new ModelRef("string")).parameterType("header").required(false);
        pars.add(parameterBuilder.build());
        parameterBuilder.name("X-userid").description("用户id").modelRef(new ModelRef("int")).parameterType("query").required(false);
        pars.add(parameterBuilder.build());
        parameterBuilder.name("X-name").description("用户名").modelRef(new ModelRef("string")).parameterType("cookie").required(false);
        pars.add(parameterBuilder.build());
        return pars;
    }

例子

文件

package com.example.lwp.swagger2.swagger2authdemo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.*;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.SecurityConfiguration;
import springfox.documentation.swagger.web.SecurityConfigurationBuilder;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.google.common.collect.Lists.newArrayList;

/**
 * @author lanwp
 * @Date 2019/9/17 7:53
 */
@Configuration
@EnableSwagger2
//@EnableSwaggerBootstrapUI
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
//                .pathMapping("/")
                .globalOperationParameters(getGlobalOperationParameters())
                .select()
                // 指定当前包路径,这里就添加了两个包,注意方法变成了basePackage,中间加上成员变量splitor
                .apis(RequestHandlerSelectors.basePackage("com.example.lwp.swagger2.swagger2authdemo.controller"))
                .paths(PathSelectors.any())
                .build()
                ;
        return docket;
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("SpringBoot整合Swagger")
                .description("SpringBoot整合Swagger,详细信息......")
                .version("9.0")
                .contact(new Contact("张三", "blog.csdn.net", "[email protected]"))
                .contact(new Contact("李四", "blog.csdn.net", "[email protected]"))
//                        .license("The Apache License")
//                        .licenseUrl("http://www.baidu.com")
                .build();
    }

    private List getGlobalOperationParameters() {
        List pars = new ArrayList<>();
        ParameterBuilder parameterBuilder = new ParameterBuilder();
        // header query cookie
        parameterBuilder.name("X-Auth-Token").description("token").modelRef(new ModelRef("string")).parameterType("header").required(false);
        pars.add(parameterBuilder.build());
        parameterBuilder.name("X-userid").description("用户id").modelRef(new ModelRef("int")).parameterType("query").required(false);
        pars.add(parameterBuilder.build());
        parameterBuilder.name("X-name").description("用户名").modelRef(new ModelRef("string")).parameterType("cookie").required(false);
        pars.add(parameterBuilder.build());
        return pars;
    }
}

页面内容
每个请求都会添加全局参数
swagger2 设置全局参数_第1张图片

你可能感兴趣的:(swagger2)