1

I am using JsonSchema2Pojo for converting json to Model objects. It creates Model objects as expecetd. But, I need the annotation @JsonIgnoreProperties(ignoreUnknown = true) as well. There is no way to have this annotation with this JsonSchema2Pojo. But, looks like it supports with the property includeAdditionalProperties. Even after setting this, I dont see additionalPropertiesMap in the generated class. Is there anythign missing here?

            <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>
joelittlejohn
  • 11,665
  • 2
  • 41
  • 54
user1578872
  • 7,808
  • 29
  • 108
  • 206

2 Answers2

1

It can be done by using a custom annotator.

Sample implementation and usage is available here.

https://github.com/thewizardofjoz/jsonschema2pojo-ignoreunknown-annotator
https://github.com/thewizardofjoz/jsonschema2pojo-example
Jose
  • 199
  • 1
  • 5
0

Set additionalproperties as true

<includeAdditionalProperties>true</includeAdditionalProperties>

This creates an additionalPropertiesMap in the generated POJO and all the properties which are unnown will be deserialised as into this map. You can use or ignore this map as per your usecase.

If you are keen on creating the annotation on the generated class (@JsonIgnoreProperties(ignoreUnknown = true), this can be done as well, Create another project or module defining the custom annotator

public class CustomAnnotator extends AbstractAnnotator {
    @Override
    public void propertyInclusion(JDefinedClass clazz, JsonNode schema) {
        clazz.annotate(JsonIgnoreProperties.class).param("ignoreUnknown", true);
    }
}

Include this annotator in your project where you are using the jsonSchema2pojo plugin.

NOTE custom-annotator is the module which has the CustomAnnotator definition.

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