我的请求对象是通过 openapi-generator-maven-plugin 基于 json 接口文件自动生成的。
我想要这个注释@JsonInclude(value = JsonInclude.Include.NON_NULL)
在自动生成的类的属性之一之上(不是所有类或类的其他属性)
以下是自动生成的:
@ApiModel(description = "blabla")
@JsonPropertyOrder({
Request.JSON_PROPERTY_CONSENT_ID,
})
@JsonTypeName("Request")
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", date = "2022-02-17T13:14:32.020579400+01:00")
public class Request{
public static final String JSON_PROPERTY_CONSENT_ID = "consentId";
private Long consentId;
@javax.annotation.Nullable
@ApiModelProperty(value = "blabla")
@JsonProperty(JSON_PROPERTY_CONSENT_ID)
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
public Long getConsentId() {
return consentId;
}
@JsonProperty(JSON_PROPERTY_CONSENT_ID)
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
public void setConsentId(Long consentId) {
this.consentId = consentId;
}
所以目前这个代码是自动生成的JsonIninclude.Include.USE_DEFAULTS但我想要的不是那个JsonIninclude.Include.NOT_NULL。这能实现吗?
我尝试使用
spring:
jackson:
default-property-inclusion: NON_NULL
在 application.yml 文件中,但结果与 USE_DEFAULTS 相同。我使用的是 Spring Boot 版本 2.1.4
我有类似的问题。我想生成类似于以下内容的模型类:
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Person {
@JsonInclude(JsonInclude.Include.ALWAYS)
private String firstName;
private String middleName;
}
我做了什么:
我使用 openapi-generator-maven-plugin 版本 > 6.0.0 支持添加到规范中的 x-field-extra-annotation:
Person:
type: object
properties:
firstName:
type: string
x-field-extra-annotation: "@JsonInclude(JsonInclude.Include.ALWAYS)"
middleName:
type: string
然后我通过以下方式添加了额外的类注释:
<configOptions>
<additionalModelTypeAnnotations>
<![CDATA[@JsonInclude(JsonInclude.Include.NON_NULL)]]>
</additionalModelTypeAnnotations>
</configOptions>
最后我删除了默认注释:
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>${replacer.version}</version>
<executions>
<execution>
<id>removeUnusedAnnotations</id>
<phase>process-resources</phase>
<goals>
<goal>replace</goal>
</goals>
<configuration>
<includes>
<include>${openapi-generator-maven-plugin.outputBaseDir}/src/**/*.java</include>
</includes>
<replacements>
<replacement>
<token>@JsonInclude\(value = JsonInclude.Include.USE_DEFAULTS\)</token>
<value />
</replacement>
</replacements>
</configuration>
</execution>
</executions>
</plugin>
最后一步是肮脏的解决方法,但找不到更好的方法。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)