我们正在使用maven插件maven-jaxb2-plugin从xsd生成JAXB对象。以下是我们的依赖
jaxb2-basics - 0.6.2
jaxb2-basics-annotate - 0.6.2
在我们的maven文件中,我们还包括-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>
绑定文件如下
<?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>
这确实添加了@JsonSerialize(使用= JsonDateSerializer.class)。但我尝试了以下几个选项来添加 include = JsonSerialize.Inclusion.NON_NULL,但无法正常工作
<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>
但在所有情况下,获取ValueParseException。那么使用include()等参数的正确方法是什么,将JsonSerialize的typing()添加到注释中。
另外,基于How to add Jackson annotations to POJO generated from XSD by JAXB/XJC? 我也试过
<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>
这也没有在注释中添加任何包含部分。
答案 0 :(得分:0)
免责声明:我是jaxb2-annotate-plugin
的作者。
首先,尝试使用XML语法(从1.0.0开始不推荐使用):
<annox:annotate
target="getter"
annox:class="org.codehaus.jackson.map.annotate.JsonSerialize"
using="org.learning.json.JsonDateSerializer"
include="NON_NULL"/>
接下来,尝试使用Java语法:
<annox:annotate>
@org.codehaus.jackson.map.annotate.JsonSerialize
(include=org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion.ALWAYS)
</annox:annotate>
你联系到的答案有一个拼写错误 - 最后)
丢失了。也许这就是问题,也许不是。
我认为这应该有效。如果没有,请在tests向我发送带有示例项目的拉取请求。我会让它发挥作用。
注意:您必须使用jaxb2-annotate-plugin
1.0.0或更高版本(当前为1.0.1)才能使用Java语法。