How to add include=JsonSerialize.Inclusion.NON_NULL to @JsonSerialize using annox plugin

2.8k views Asked by At

We are generating JAXB objects from xsd, using maven plugin maven-jaxb2-plugin. Below are dependencies we have

jaxb2-basics - 0.6.2
jaxb2-basics-annotate - 0.6.2

In our maven file, we also included -Xannotate and -XtoString

    <plugin>                
<groupId>org.jvnet.jaxb2.maven2</groupId>
                    <artifactId>maven-jaxb2-plugin</artifactId>
                    <executions>
                         <execution>
                            <id>exec1</id>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>generate</goal>
                            </goals>
                            <configuration>
                                <schemaDirectory>${basedir}/src/main/resources/xsd</schemaDirectory>
                                <bindingDirectory>${basedir}/src/main/resources/xsd</bindingDirectory>
                                <generatePackage>org.learning.json.generated</generatePackage>
                                <generateDirectory>${basedir}/generated</generateDirectory>
                                <clearOutputDir>false</clearOutputDir>
                                <includeSchemas>
                                    <includeSchema>Person.xsd</includeSchema>
                                </includeSchemas>
                                <plugins>
                                    <plugin>
                                        <groupId>org.jvnet.jaxb2_commons</groupId>
                                        <artifactId>jaxb2-basics</artifactId>
                                        <version>0.6.2</version>
                                    </plugin>
                                    <plugin>
                                        <groupId>org.jvnet.jaxb2_commons</groupId>
                                        <artifactId>jaxb2-basics-annotate</artifactId>
                                        <version>0.6.2</version>
                                    </plugin>
                                </plugins>
                                <args>
                                    <arg>-Xannotate</arg>
                                    <arg>-XtoString</arg>
                                </args>
                            </configuration>
                        </execution>

The binding file is as below

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
               xmlns:xs="http://www.w3.org/2001/XMLSchema" 
               xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
               xmlns:annox="http://annox.dev.java.net"
               jaxb:extensionBindingPrefixes="xjc annox"
               jaxb:version="2.0">

     <jaxb:bindings schemaLocation="Person.xsd" multiple="true">  
        <jaxb:bindings node="xs:complexType[@name='personType']/xs:sequence/xs:element[@type='xs:date']" multiple="true">
            <annox:annotate>
                <annox:annotate target="getter" annox:class="org.codehaus.jackson.map.annotate.JsonSerialize" 
                                using="org.learning.json.JsonDateSerializer"/>
            </annox:annotate>
        </jaxb:bindings>
     </jaxb:bindings>
</jaxb:bindings>

This did add @JsonSerialize(using=JsonDateSerializer.class). But I tried few options like below to add include=JsonSerialize.Inclusion.NON_NULL, but did not work

<annox:annotate>
                <annox:annotate target="getter" annox:class="org.codehaus.jackson.map.annotate.JsonSerialize" 
                                using="org.learning.json.JsonDateSerializer"
        include="org.codehause.jackson.map.annotate.JsonSerialize.Inclusion.NON_NULL"/>
            </annox:annotate> 



<annox:annotate>
                <annox:annotate target="getter" annox:class="org.codehaus.jackson.map.annotate.JsonSerialize" 
                                using="org.learning.json.JsonDateSerializer"
        include="org.codehause.jackson.map.annotate.JsonSerialize$Inclusion.NON_NULL"/>
            </annox:annotate> 

But in all cases, getting ValueParseException. So what is the correct way of having parameters like include(), typing() of JsonSerialize be added to annotation.

Also, based on How to add Jackson annotations to POJO generated from XSD by JAXB/XJC? I also tried

<jaxb:bindings schemaLocation="Person.xsd" multiple="true">  
        <jaxb:bindings node="xs:complexType[@name='personType']/xs:sequence/xs:element[@type='xs:date']" multiple="true">
            <annox:annotate>
                <annox:annotate target="getter" annox:class="org.codehaus.jackson.map.annotate.JsonSerialize" 
                                using="org.learning.json.JsonDateSerializer"/>
            </annox:annotate>
            <annox:annotate>
                    @org.codehaus.jackson.map.annotate.JsonSerialize
                    (include=org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion.ALWAYS
            </annox:annotate>
        </jaxb:bindings>
     </jaxb:bindings>

This also did not add any include part in the annotation.

1

There are 1 answers

1
lexicore On

Disclaimer: I am the author of the jaxb2-annotate-plugin.

First of all, try this for the XML syntax (which is deprecated starting from 1.0.0):

<annox:annotate
    target="getter"
    annox:class="org.codehaus.jackson.map.annotate.JsonSerialize" 
    using="org.learning.json.JsonDateSerializer"
    include="NON_NULL"/>

Next, try this for the Java syntax:

<annox:annotate>
    @org.codehaus.jackson.map.annotate.JsonSerialize
        (include=org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion.ALWAYS)
</annox:annotate>

The answer you linked to had a typo - the last ) was missing. Maybe this is the problem, maybe not.

I think this should work. If it does not, please send me a pull request with a sample project in tests. I'll make it work.

Note: You'll have to use jaxb2-annotate-plugin 1.0.0 or later (the current is 1.0.1) for Java syntax to work.