如何使用JAXB生成一个从xsd实现Serializable接口的Java类?

时间:2009-10-03 15:01:07

标签: java xsd jaxb xjc

我想将缓存引入现有的Spring项目,该项目使用JAXB来公开WebServices。缓存将在端点级别完成。为了做到这一点,使用JAXB从XSD生成的类需要实现Serializable接口并覆盖Object的{​​{1}}方法。

如何使用XSD指示xjc工具生成具有所需属性的源?

6 个答案:

答案 0 :(得分:75)

序列化

在自定义绑定文件中使用xjc:serializablejava.io.Serializable界面添加到您的课程以及serialVersionUID

<?xml version="1.0" encoding="UTF-8"?>
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
            xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
            xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
            xsi:schemaLocation="
http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
version="2.1">
  <globalBindings>
    <serializable uid="1" />
  </globalBindings>
</bindings> 

的toString()

使用超类(请参阅xjc:superClass),您的所有绑定类将从该超类继承。 xjc不会生成此类,因此您可以随意创建它(此处使用toString()实现):

<?xml version="1.0" encoding="UTF-8"?>
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
                xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
                xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
                xsi:schemaLocation="
http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
    version="2.1">
    <globalBindings>
        <serializable uid="1" />
        <xjc:superClass name="the.package.to.my.XmlSuperClass" />
    </globalBindings>
</bindings>

答案 1 :(得分:7)

这对我有用:

<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" jaxb:version="2.1">
    <jaxb:globalBindings>
        <xjc:serializable uid="1337"/>
    </jaxb:globalBindings>
</jaxb:bindings>

希望它有所帮助。

答案 2 :(得分:5)

另一种生成toString()方法的方法 - JAXB2 Basics Plugins。这种方式看起来更好,因为不使用反射。以下是如何使用maven的示例。

<build>
    <plugins>
        <plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <version>0.8.2</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <schemaDirectory>
                    ${project.basedir}/src/main/resources
                </schemaDirectory>
                <generateDirectory>
                    ${project.basedir}/src/main/java
                </generateDirectory>
                <extension>true</extension>
                <args>
                    <arg>-XtoString</arg>
                </args>
                <plugins>
                    <plugin>
                        <groupId>org.jvnet.jaxb2_commons</groupId>
                        <artifactId>jaxb2-basics</artifactId>
                        <version>0.6.4</version>
                    </plugin>
                </plugins>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>org.jvnet.jaxb2_commons</groupId>
        <artifactId>jaxb2-basics-runtime</artifactId>
        <version>0.6.4</version>
    </dependency>
</dependencies>

结果你会得到这样的代码。

public String toString() {
    final ToStringStrategy strategy = JAXBToStringStrategy.INSTANCE;
    final StringBuilder buffer = new StringBuilder();
    append(null, buffer, strategy);
    return buffer.toString();
}

public StringBuilder append(ObjectLocator locator, StringBuilder buffer, ToStringStrategy strategy) {
    strategy.appendStart(locator, this, buffer);
    appendFields(locator, buffer, strategy);
    strategy.appendEnd(locator, this, buffer);
    return buffer;
}

public StringBuilder appendFields(ObjectLocator locator, StringBuilder buffer, ToStringStrategy strategy) {
    {
        String theName;
        theName = this.getName();
        strategy.appendField(locator, this, "name", buffer, theName);
    }
    {
        String theBank;
        theBank = this.getBank();
        strategy.appendField(locator, this, "bank", buffer, theBank);
    }
    return buffer;
}

答案 3 :(得分:2)

我使用此代码并为我工作

<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" jaxb:version="2.1">
    <jaxb:globalBindings>
        <xjc:serializable uid="1"/><!--If you Forgot this line your class did not be serializable--!>
    </jaxb:globalBindings>
</jaxb:bindings>

答案 4 :(得分:1)

为了获得接口Serializable,将以下绑定信息添加到xsd-file:

<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.0"> <xs:annotation> <xs:appinfo> <jaxb:globalBindings optionalProperty="primitive"> <jaxb:serializable/> </jaxb:globalBindings> </xs:appinfo> </xs:annotation> <xs:element name="myXsdElement"> ..... </xs:element> </xs:schema>

答案 5 :(得分:1)

以下是使用CXF 3.1.10的示例。请记住包含编译组:&#39; org.apache.cxf.xjc-utils&#39;,名称:&#39; cxf-xjc-runtime&#39;,版本:&#39; 3.1.0&#39;

<jxb:bindings 
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
version="2.1">

<jxb:bindings schemaLocation="META-INF/wsdl/xsd2.xsd">

        <jxb:globalBindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema">
                <jxb:serializable uid="1"/>
              </jxb:globalBindings>