Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swagger Code Gen - package javax.annotation does not exist

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
like image 519
quarks Avatar asked Mar 07 '26 07:03

quarks


2 Answers

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>
like image 175
Baptiste Pernet Avatar answered Mar 09 '26 20:03

Baptiste Pernet


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

like image 30
quarks Avatar answered Mar 09 '26 19:03

quarks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!