Swagger Code Gen - package javax.annotation does not exist
Asked Answered
R

3

10

Why is swagger-codegen generating project with missing dependencies?

Running: java -jar swagger-codegen-cli.jar generate -l java -i swagger.json

Generates a project with this for example:

@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2021-04-27T18:37:06.211+08:00")
public class Table {
  @SerializedName("requiredIndexColumns")
  private List<Column> requiredIndexColumns = null;

Where javax.annotation.Generate cannot be resolved. And then compiling the generated project throws: Error:(33,18) java: package javax.annotation does not exist

Here's how to test this (using a public swagger):

wget https://repo1.maven.org/maven2/io/swagger/swagger-codegen-cli/2.4.19/swagger-codegen-cli-2.4.19.jar -O swagger-codegen-cli.jar

Then run:

java -jar swagger-codegen-cli.jar generate -l java -i https://petstore.swagger.io/v2/swagger.json -o petstore
Revegetate answered 27/4, 2021 at 10:53 Comment(1)
What is your Java version? javax.annotation.Generated seems to exists in Java 10, but no longer in Java 11. I expect you'll need to add either JSR 250 Common Annotations For The JavaTM Platform or Javax Annotation APINeau
R
6

The solution is to run with this command:

java -jar swagger-codegen-cli.jar generate -l java -i https://petstore.swagger.io/v2/swagger.json -o petstore -DhideGenerationTimestamp=true

The option to turn off @javax.annotation.Generated date in Java Classes

Revegetate answered 27/4, 2021 at 12:19 Comment(2)
@Edd There is no version 6.2.0. Current version is 3.0.54, see github.com/swagger-api/swagger-codegen/releasesEye
Not a full solution, if your spec contains nullability information that might get translated into javax.annotation.Nullable and you are back to square 1.Spatz
M
13

Formalizing the comment left by Scratte, if you are using jdk11, you need to add the dependency explicitly in your dependency management tool

For example in maven

    <dependency>
      <groupId>javax.annotation</groupId>
      <artifactId>javax.annotation-api</artifactId>
      <version>${javax-annotation-api.version}</version>
    </dependency>
Moue answered 9/7, 2021 at 22:44 Comment(1)
mvnrepository.com/artifact/javax.annotation/…Odious
R
6

The solution is to run with this command:

java -jar swagger-codegen-cli.jar generate -l java -i https://petstore.swagger.io/v2/swagger.json -o petstore -DhideGenerationTimestamp=true

The option to turn off @javax.annotation.Generated date in Java Classes

Revegetate answered 27/4, 2021 at 12:19 Comment(2)
@Edd There is no version 6.2.0. Current version is 3.0.54, see github.com/swagger-api/swagger-codegen/releasesEye
Not a full solution, if your spec contains nullability information that might get translated into javax.annotation.Nullable and you are back to square 1.Spatz
U
1

If you are using swagger-codegen-maven-plugin, you can avoid this problem by disabling @Generated annotations using this configuration:

<configOptions>
    <hideGenerationTimestamp>true</hideGenerationTimestamp>
</configOptions>
Utta answered 10/5 at 15:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.