Swagger Code Gen - package javax.annotation does not exist Swagger代码生成器 - 包javax.annotation不存在

9

为什么 Swagger Codegen 生成的项目缺少依赖项?

运行以下命令:

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

将生成一个示例项目:

@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;

无法解析javax.annotation.Generate。编译生成的项目时会抛出以下错误:Error:(33, 18) java: package javax.annotation does not exist

以下是如何测试此问题(使用公共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

然后运行:

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

3
你的Java版本是什么?javax.annotation.Generated似乎存在于Java 10中,但在Java 11中不再存在。我认为你需要添加JSR 250 Common Annotations For The JavaTM PlatformJavax Annotation API中的一个。 - Scratte
2个回答

13
正式化Scratte留下的评论,如果您正在使用jdk11,您需要在依赖管理工具中明确添加dependency
例如,在Maven中。
    <dependency>
      <groupId>javax.annotation</groupId>
      <artifactId>javax.annotation-api</artifactId>
      <version>${javax-annotation-api.version}</version>
    </dependency>

1
https://mvnrepository.com/artifact/javax.annotation/javax.annotation-api - Jasper de Vries

5
解决方案是运行以下命令:

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

选项用于在Java类中关闭@javax.annotation.Generated日期。

你应该接受这个(你自己的)答案 :) - Honza Zidek
7
这在新版本中不起作用(已在6.2.0上检查)。时间戳被隐藏,但注释仍然生成。 - Edd

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接