微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

openapi-generator 不会使用 Spring Webclient 为 Java 客户端生成响应头

如何解决openapi-generator 不会使用 Spring Webclient 为 Java 客户端生成响应头

我们有一个声明响应头的 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,String> cookieParams,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。

我错过了什么吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。