swagger加請求頭(測試帶token的請求)

現在大家一般都是使用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(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.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();
    }
}

效果圖:

 

get /homie/confirmCheckMessage

Response Class (Status 200)

OK

 

{
  "code": 0,
  "data": {},
  "msg": "string"
}

 

Response Content Type

Parameters

Parameter Value Description Parameter Type Data Type
Authorization  

令牌

header string
id  

id

query string
friendAccount  

friendAccount

query string

截圖失敗了, 將就看一下, 多出來一個 Authorization,在裏面放token就行了.

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章