swagger(测试带token的请求){ "code": 401, "msg": "Validate token fail" }

原文链接:https://blog.csdn.net/ssjq123/article/details/82592022

现在大家一般都是使用SpringBoot写RESTful接口, 但是在测试带有token的请求的时候, 就有点难受了. 传统的PostMan就有点让人炸毛了.  但是现在Swagger出现了.(SpringBoot简直和Swagger是天作之和)

swagger的整合细节就不在这里说了, 下面进入正题:
 

@Configuration
public class Swagger2Config {
    @Bean
    public Docket createRestApi() {
//
        //添加head参数配置start
        ParameterBuilder tokenPar = new ParameterBuilder();  
        List<Parameter> pars = new ArrayList<>();
        tokenPar.name("Authorization")
                .description("令牌")
                .modelRef(newModelRef("string"))
                .parameterType("header")
                .required(false).build();

        //添加head参数配置end
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.cinsc.MainView.ctr"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars);//重点看:这里
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("springboot利用swagger构建api文档")
                .description("束手就擒--简单优雅的restfun风格")
                .termsOfServiceUrl("https://blog.csdn.net/ssjq123")
                .version("1.0")
                .build();
    }
}

最终显示效果:Authorization


版权声明:本文为zhangxiaojun211原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。