我正在使用jsonschema2pojo将JSON转换为模型对象。它将模型对象作为expecetd创建。但是,我也需要注释@jsonignoreproperties(ignoreunknown = true)。这个Jsonschema2pojo无法使用此注释。但是,看起来它支持该属性包括eadditionalProperties。即使设置了此功能,我也看不到生成类中的其他PropertiesMap。这里是否缺少任何标签?
<plugin>
<groupId>org.jsonschema2pojo</groupId>
<artifactId>jsonschema2pojo-maven-plugin</artifactId>
<version>0.5.1</version>
<configuration>
<sourceDirectory>${project.parent.basedir}/app-service/src/main/resources/schema</sourceDirectory>
<targetPackage>com.xyz.test.dto</targetPackage>
<annotationStyle>jackson2</annotationStyle>
<generateBuilders>true</generateBuilders>
<useCommonsLang3>true</useCommonsLang3>
<useLongIntegers>false</useLongIntegers>
<includeJsr303Annotations>true</includeJsr303Annotations>
<includeAdditionalProperties>false</includeAdditionalProperties>
</configuration>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
可以使用自定义注释。
来完成示例实现和用法可在此处提供。
https://github.com/thewizardofjoz/jsonschema2pojo-ignoreunknown-annotator
https://github.com/thewizardofjoz/jsonschema2pojo-example
将额外的Properties设置为true
<includeAdditionalProperties>true</includeAdditionalProperties>
这将在生成的pojo中创建一个额外的propertiesmap,并且所有未纳入的属性都将被视为在此地图中。您可以根据用户酶使用或忽略此地图。
如果您热衷于在生成类中创建注释(@jsonignoreproperties(ignoreunknown = true),这也可以完成创建另一个项目或定义自定义注释器的模块
public class CustomAnnotator extends AbstractAnnotator {
@Override
public void propertyInclusion(JDefinedClass clazz, JsonNode schema) {
clazz.annotate(JsonIgnoreProperties.class).param("ignoreUnknown", true);
}
}
在您使用Jsonschema2pojo插件的项目中包含此注释。
Note 自定义通知器是具有自定义定义的模块。
<plugin>
<groupId>org.jsonschema2pojo</groupId>
<artifactId>jsonschema2pojo-maven-plugin</artifactId>
<version>1.0.2</version>
<configuration>
<sourceDirectory>${basedir}</sourceDirectory>
<includes>src/main/resources/schema/*.json</includes>
<targetPackage>com.example.result</targetPackage>
<customAnnotator>com.example.annotations.CustomAnnotator</customAnnotator>
</configuration>
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>custom-annotator</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>