Swagger codegen 忽略 POJO 的空字段 类
Swagger codegen ignore null fields for POJO classes
我正在做一个 REST API,我想在 class 代添加 Jackson 注释以忽略空字段,所以我想只为某些 classes,不适用于 hole 项目。
我知道这可以通过在 application.properties 中添加以下行来完成:
spring.jackson.default-property-inclusion=non_null
但这是针对整个项目的。
我看到有一些“.mustache”文件
- api.mustache
- apiController.mustache
我想我必须在其中一个中添加一些代码,或者我应该在 application.yml 中插入一些代码吗?
我也在使用 Spring Boot with Swagger-codgen。
提前谢谢你,祝你有愉快的一天!
好的,经过几个小时的研究,我发现因为我使用的是 Swagger-codegen,所以我必须在 https://github.com/swagger-api/swagger-codegen 中搜索有关 Swagger-Codegen 的所有答案。这是所有模板,我发现我需要将以下两个文件添加到我的项目中
- pojo.mustache
- xmlAnnotation.mustache
上述文件所在路径为
swagger-codegen/modules/swagger-codegen/src/main/resources/JavaSpring/
不仅如此,这些文件是生成 Pojo 类 的简单模板,因此您需要在 pojo.mustache 文件中添加 @JsonInclude(JsonInclude.Include.NON_NULL)
注释,在带有 public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}}...
以便在生成 pojo 时插入 类.
完成,再次构建您的项目! :)
您可以尝试在 application.yaml
中设置以下属性
spring.jackson.default-property-inclusion = NON_NULL
这对我有用,可以从响应中过滤掉空值。
我正在做一个 REST API,我想在 class 代添加 Jackson 注释以忽略空字段,所以我想只为某些 classes,不适用于 hole 项目。
我知道这可以通过在 application.properties 中添加以下行来完成:
spring.jackson.default-property-inclusion=non_null
但这是针对整个项目的。
我看到有一些“.mustache”文件
- api.mustache
- apiController.mustache
我想我必须在其中一个中添加一些代码,或者我应该在 application.yml 中插入一些代码吗?
我也在使用 Spring Boot with Swagger-codgen。
提前谢谢你,祝你有愉快的一天!
好的,经过几个小时的研究,我发现因为我使用的是 Swagger-codegen,所以我必须在 https://github.com/swagger-api/swagger-codegen 中搜索有关 Swagger-Codegen 的所有答案。这是所有模板,我发现我需要将以下两个文件添加到我的项目中
- pojo.mustache
- xmlAnnotation.mustache
上述文件所在路径为
swagger-codegen/modules/swagger-codegen/src/main/resources/JavaSpring/
不仅如此,这些文件是生成 Pojo 类 的简单模板,因此您需要在 pojo.mustache 文件中添加 @JsonInclude(JsonInclude.Include.NON_NULL)
注释,在带有 public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}}...
以便在生成 pojo 时插入 类.
完成,再次构建您的项目! :)
您可以尝试在 application.yaml
spring.jackson.default-property-inclusion = NON_NULL
这对我有用,可以从响应中过滤掉空值。