【问题标题】:Add lombok (or any) annotation to swagger generated class将 lombok(或任何)注释添加到 swagger 生成的类
【发布时间】:2020-02-16 10:24:43
【问题描述】:

我搜索了互联网,但没有找到任何解决此问题的方法。 是否可以在 swagger 类 UPON 生成中添加 lombok 注释?

我有这个招摇模式:

Product:
type: "object"
required:
  - idSeller
  - model
  - name
  - description
  - price
properties:
  id:
    type: string
  idSeller:
    type: string
  model:
    type: string
  name:
    type: string
  description:
    type: string
  price:
    type: number
    format: currency
    minimum: 0.01

生成此代码:

@Validated
@javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2019-10-18T20:36:31.834-03:00")

public class Product   {
    @JsonProperty("id")
    private String id = null;

    @JsonProperty("idSeller")
    private String idSeller = null;

    //rest of the code ommited
} 

但我需要它来生成这个:

@Validated
@javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2019-10-18T20:36:31.834-03:00")

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Product   {
    @JsonProperty("id")
    private String id = null;

    @JsonProperty("idSeller")
    private String idSeller = null;

    //rest of the code ommited
}

使用 lombok Data、Builder、NoArgsConstructor 和 AllArgsConstructor 注释。

你能帮我解决这个问题吗?

【问题讨论】:

  • 如果您使用允许它的构建工具,您可以编写一个简单的插件来读取您生成的文件并将注释添加到它们。我可以看到使用 gradle 很容易做到这一点

标签: java spring-boot swagger-2.0 lombok


【解决方案1】:

怎么样

perl -pi -e 's/(?=^public class )/\@Data\n\@Builder\n\@NoArgsConstructor\n\@AllArgsConstructor\n/' *.java

?我想,这不是您所期望的,但您可以将其集成到您的构建过程中,并拥有一个经得起时间考验的解决方案。

实际上,您也需要导入,但这同样简单。

【讨论】:

  • 我建议使用像 javaparser 这样的实际 Java 文件解析器,它适用于迄今为止的所有 Java 版本
  • @smac89 当然,你的解决方案是更干净的解决方案……我的解决方案很大程度上取决于了解文件的外观。我将 perl 用于琐碎的任务,到目前为止这已经足够好了。当更大的事情发生时,我也会使用适当的解析器。
猜你喜欢
  • 2021-09-19
  • 1970-01-01
  • 2020-08-05
  • 2018-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-08
  • 1970-01-01
相关资源
最近更新 更多