json - How to add include=JsonSerialize.Inclusion.NON_NULL to @JsonSerialize using annox plugin -
we generating jaxb objects xsd, using maven plugin maven-jaxb2-plugin. below dependencies have
jaxb2-basics - 0.6.2
jaxb2-basics-annotate - 0.6.2
in our maven file, included -xannotate , -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 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). tried few options below add include=jsonserialize.inclusion.non_null, 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 cases, getting valueparseexception. correct way of having parameters include(), typing() of jsonserialize added annotation.
also, based on how add jackson annotations pojo generated xsd jaxb/xjc? 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 did not add include part in annotation.
disclaimer: author of jaxb2-annotate-plugin
.
first of all, try xml syntax (which deprecated starting 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 java syntax:
<annox:annotate> @org.codehaus.jackson.map.annotate.jsonserialize (include=org.codehaus.jackson.map.annotate.jsonserialize.inclusion.always) </annox:annotate>
the answer linked had typo - last )
missing. maybe problem, maybe not.
i think should work. if not, please send me pull request sample project in tests. i'll make work.
note: you'll have use jaxb2-annotate-plugin
1.0.0 or later (the current 1.0.1) java syntax work.
Comments
Post a Comment