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();
}
效果如下:
同样的可以直接使用注解@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<Parameter> 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();
}
}
效果图