简体   繁体   English

Java REST Client的swagger-codegen头参数

[英]swagger-codegen header parameter for Java REST Client

I am using swagger-codegen for generating a Java REST client for one of my REST APIs. 我使用swagger-codegen为我的一个REST API生成Java REST客户端。 The REST APIs take an optional header parameter. REST API采用可选的头参数。 The generated methods in the client have an additional parameter that takes the header. 客户端中生成的方法有一个额外的参数,用于获取标头。 I would like the methods to be generated without the header parameter in the method signature. 我想在方法签名中没有header参数的情况下生成方法。 I have read the documentation, but couldn't find any reference. 我已经阅读了文档,但找不到任何参考。

For example, for a GET all API with option X-CUSTOM-HEADER parameter, swagger-codegen generates a method like below: 例如,对于带有选项X-CUSTOM-HEADER参数的GET所有API,swagger-codegen生成如下方法:

public List<SomeType> findAllUsingGET1(String optionalHeader)

where as I would like it to be: 我希望它在哪里:

public List<SomeType> findAllUsingGET1()

Looking for pointers for the workaround rather than customizing the client-code generation. 寻找变通方法的指针,而不是自定义客户端代码生成。

EDIT 1: Adding the JSON spec 编辑1:添加JSON规范

  "get": {
    "summary": "findAll",
    "operationId": "findAllUsingGET1",
    "consumes": [
      "application/json"
    ],
    "produces": [
      "application/json"
    ],
    "parameters": [
      {
        "name": "X-CUSTOM-HEADER",
        "in": "header",
        "description": "Custom Header",
        "required": false,
        "type": "string"
      }
    ],
    "responses": {
      "200": {
        "description": "OK",
        "schema": {
          "type": "string"
        }
      },
      "401": {
        "description": "Unauthorized"
      },
      "403": {
        "description": "Forbidden"
      },
      "404": {
        "description": "Not Found"
      }
    }
  }

If you want to remove a parameter (optional) from the method signature in the Java API client, the only way is to remove the parameter from the Swagger/OpenAPI specification. 如果要从Java API客户端中的方法签名中删除参数(可选),唯一的方法是从Swagger / OpenAPI规范中删除该参数。

To add default headers, you can use the addDefaultHeader method in ApiClient : https://github.com/swagger-api/swagger-codegen/blob/master/samples/client/petstore/java/okhttp-gson/src/main/java/io/swagger/client/ApiClient.java#L528 要添加默认标题,您可以使用addDefaultHeader的方法ApiClienthttps://github.com/swagger-api/swagger-codegen/blob/master/samples/client/petstore/java/okhttp-gson/src/main/爪哇/ IO /昂首阔步/客户/ ApiClient.java#L528

UPDATE: Header parameters, similar to form, query parameters, are generated as method arguments. 更新:类似于表单,查询参数的标头参数作为方法参数生成。 From the developer perspective, it's just another parameter (and they do not need to know whether the parameter is a header, form or query parameter) 从开发人员的角度来看,它只是另一个参数(并且他们不需要知道参数是标头,表单还是查询参数)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM