【发布时间】:2021-07-29 06:41:39
【问题描述】:
我们有一个声明响应头的 openapi 文件(已验证),例如
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "object",
"properties": {
"results": {
"type": "array",
"items": {
"$ref": "#/definitions/CourseV2"
}
},
"paging": {
"$ref": "#/definitions/PagingInfo"
}
},
"required": [
"results"
]
},
"headers": {
"X-RateLimit-Limit": {
"type": "integer",
"description": "Request limit per hour."
},
"X-RateLimit-Remaining": {
"type": "integer",
"description": "The number of requests left for the time window."
},
"X-RateLimit-Reset": {
"type": "string",
"format": "date-time",
"description": "The UTC date/time at which the current rate limit window resets."
}
}
},
使用openapi-generator项目(maven或cli)生成的java webclient客户端,调用的返回类型不包括响应头,ApiClient类中的invokeAPI方法也不提供对响应头的访问。 实际上,ApiClient 调用方法正在返回返回类型的 Mono/Flux,因此不允许获取响应标头
public <T> Mono<T> invokeAPI(String path, HttpMethod method, Map<String, Object> pathParams, MultiValueMap<String, String> queryParams, Object body, HttpHeaders headerParams, MultiValueMap<String, String> cookieParams, MultiValueMap<String, Object> formParams, List<MediaType> accept, MediaType contentType, String[] authNames, ParameterizedTypeReference<T> returnType) throws RestClientException {
final WebClient.RequestBodySpec requestBuilder = prepareRequest(path, method, pathParams, queryParams, body, headerParams, cookieParams, formParams, accept, contentType, authNames);
return requestBuilder.retrieve().bodyToMono(returnType);
}
据我所知,使用 Spring Webclient 访问响应标头的唯一方法是使用 toEntity,它提供 ResponseEntity 方法(以及 .getHeaders)而不是 bodyToMono。
我错过了什么吗?
【问题讨论】:
-
您找到解决方案了吗?我看到了类似的问题。
-
@Upen 看看我下面的评论
标签: java spring-webflux spring-webclient openapi-generator