Swagger Header单个接口及其全局接口添加

1.单个接口header设置

直接使用swagger的注解ApiImplicitParam , 如下
    @ApiOperation(value = "用户",notes = "测试用户枚举")
    @ApiImplicitParam(name = "app_token", value = "app_token",required = true, dataType = "String",paramType="header")
    @RequestMapping(value = "/test6" , method = RequestMethod.POST)
    public String test6(HttpServletRequest request,@RequestBody User1 user) {
        String app_token = request.getHeader("app_token");
        System.out.println(user+"+"+app_token);
        return user.getName();
    }

效果如下:


WX20190617-152112.png

同样的可以直接使用注解@RequestHeader代替

    @ApiOperation(value = "用户",notes = "测试")
    @RequestMapping(value = "/test7" , method = RequestMethod.POST)
    public String test7(@RequestBody User1 user, @RequestHeader(required = false) String app_token) {
        System.out.println(user+"+"+app_token);
        return user.getName();
    }

2.全局接口header设置

在swagger中添加全局header配置 , 如下

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        ParameterBuilder aParameterBuilder = new ParameterBuilder();
        aParameterBuilder.name("token").description("token").modelRef(new ModelRef("string")).parameterType("header").required(true).build();

        List aParameters = Lists.newArrayList();
        aParameters.add(aParameterBuilder.build());
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                .globalOperationParameters(aParameters)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.demo1.controller"))
                .paths(PathSelectors.any()).build();
    }
}

效果图


WX20190617-151844.png

你可能感兴趣的:(Swagger Header单个接口及其全局接口添加)