swagger3配置header
时间: 2024-01-17 11:16:02 浏览: 126
swagger配置类
在Swagger3中配置header可以通过以下步骤实现:
1. 在SwaggerConfig类中添加如下代码:
```java
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo"))
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo())
.securitySchemes(Arrays.asList(apiKey()));
}
private ApiKey apiKey() {
return new ApiKey("apiKey", "X-API-KEY", "header");
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("API Documentation")
.description("API Documentation")
.version("1.0.0")
.build();
}
```
2. 在Controller中添加如下注解:
```java
@ApiOperation(value = "API接口", notes = "API接口")
@ApiImplicitParams({
@ApiImplicitParam(name = "X-API-KEY", value = "API Key", required = true, dataType = "string", paramType = "header")
})
@GetMapping("/api")
public String api() {
return "Hello World!";
}
```
其中,@ApiImplicitParam注解用于描述请求参数,paramType属性设置为header即可。
阅读全文