问题
I am trying to generate a client using swagger codegen for a REST Api having OAuth 2.0 security. 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.
The following combinations I have tried for the -a option in the above command:
-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
回答1:
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"
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:
- Java 7 or 8
- Apache maven 3.3.3 or greater
The following commands can be used to build the codegen from the source
- git clone https://github.com/swagger-api/swagger-codegen
- cd swagger-codegen
- mvn clean package
You can also download the JAR (latest release) directly from maven.org
Generating code
You can generate the code for a particular JSON that you generated from the Swagger Editor using the following command.
- java -jar /swagger-codegen-cli.jar generate -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
The generated code has a maven build, so the library dependencies of the code is specified in the pom.xml.
来源:https://stackoverflow.com/questions/29537222/swagger-codegen-is-not-working-while-generating-java-client-for-a-rest-api-havin