我们使用maven插件maven-jaxb2-plugin从xsd生成JAXB对象。下面是我们的依赖项
Jaxb2-基础- 0.6.2
jaxb2 2-基础-注释- 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(using=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。因此,将JsonSerialize的include()、the ()等参数添加到注释中的正确方法是什么。
而且,基于如何将Jackson注释添加到JAXB/XJC从XSD生成的POJO中?,我也尝试了
<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>这也没有在注释中添加任何包含部分。
发布于 2014-12-03 07:53:27
免责声明:,我是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>你链接到的答案有一个错误-上一个)丢失了。也许这就是问题所在,也许不是。
我觉得这个应该管用。如果没有,请在测试中向我发送一个带有示例项目的拉请求。我会让它发挥作用的。
注意:为了使语法工作,您必须使用jaxb2-annotate-plugin 1.0.0或更高版本(当前是1.0.1)。
https://stackoverflow.com/questions/27244008
复制相似问题