简体   繁体   English

Swagger Codegen在为具有OAuth2.0安全性的Rest API生成Java客户端时无法正常工作

[英]Swagger Codegen is not working while generating java client for a Rest API having OAuth2.0 security

I am trying to generate a client using swagger codegen for a REST Api having OAuth 2.0 security. 我正在尝试为具有OAuth 2.0安全性的REST Api使用swagger代码生成客户端。 The call is following: 呼叫如下:

    E:\>java -jar swagger-codegen-distribution-2.1.2-M1.jar  -a access_token:df8eb092-e34f-48f9-b4ce-32cdd1ae2613 -i http://localhost:8080/someapi/api-docs -l java -o E:\projects\prototypes\ApiClient

But this call is not working and generating the following output as: 但是此调用无法正常工作,并生成以下输出:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
reading from http://localhost:8080/someapi/api-docs
reading from http://localhost:8080/someapi/api-docs
usage: Codegen
-a,--auth                 addes authorization headers when fetching the
                       swagger definitions remotely. Pass in a
                       URL-encoded string of name:header with a comma
                       separating multiple values
-d,--debug-info           prints additional info for debugging
-h,--help                 shows this message
-i,--input-spec <arg>     location of the swagger spec, as URL or file
-l,--lang <arg>           client language to generate.
                       Available languages include:
                       [android, async-scala, java, jaxrs, nodejs,
                       objc, scalatra, scala, dynamic-html, html,
                       swagger, tizen, php, python]
-o,--output <arg>         where to write the generated files
-t,--template-dir <arg>   folder containing the template files

I have implemented recently the OAuth2.0 for this API so I assume that there is some problem with the authentication header I am setting with this APi as earlier the codegen was working well. 我最近已经为此API实现了OAuth2.0,因此我认为我使用此APi设置的身份验证标头存在一些问题,因为之前的代码生成很好。

The following combinations I have tried for the -a option in the above command: 我在上面的命令中为-a选项尝试了以下组合:

 -a access_token:<token>
 -a "access_token":<token>
 -a name:AUTHORIZATION,<token>

But nothing is working. 但是没有任何效果。 I am really in trouble with a demo today. 我今天的演示真的很麻烦。 Really appreciate any quick help 非常感谢任何快速帮助

Thanks 谢谢

I have been trying the same thing myself and I will post what I think your issue is. 我自己一直在尝试相同的事情,因此我将发布我认为您的问题所在。 Please try the command in the format given below. 请尝试使用以下格式的命令。

java -jar <Path of codegen jar>/swagger-codegen-cli.jar generate -i <path of swagger JSON>/swagger.json -l java -o <Output path>

In your command I do not see the JSON file name in " http://localhost:8080/someapi/api-docs " 在您的命令中,我在“ http:// localhost:8080 / someapi / api-docs中看不到JSON文件名

If this does not work here is what I did to make the codegen work sucessfully. 如果这不起作用,我要做的就是使代码生成成功进行。

Installation To build from source, you need the following installed and available in your $PATH: 安装要从源代码进行构建,您需要安装以下文件并在$ PATH中可用:

  • Java 7 or 8 Java 7或8
  • Apache maven 3.3.3 or greater Apache Maven 3.3.3或更高版本

The following commands can be used to build the codegen from the source 以下命令可用于从源代码构建代码生成

You can also download the JAR (latest release) directly from maven.org 您也可以直接从maven.org下载JAR(最新版本)

Generating code 产生程式码

You can generate the code for a particular JSON that you generated from the Swagger Editor using the following command. 您可以使用以下命令为从Swagger编辑器生成的特定JSON生成代码。

  • java -jar /swagger-codegen-cli.jar generate -i /swagger.json -l java -o java -jar /swagger-codegen-cli.jar生成-i /swagger.json -l Java -o

Additional details in relation to this can be found in the link https://github.com/swagger-api/swagger-codegen/blob/master/README.md#compatibility 与此相关的其他详细信息可以在链接https://github.com/swagger-api/swagger-codegen/blob/master/README.md#compatibility中找到

The generated code has a maven build, so the library dependencies of the code is specified in the pom.xml. 生成的代码具有Maven构建,因此该代码的库依赖关系在pom.xml中指定。

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

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