45

我想将缓存引入到使用 JAXB 公开 WebServices 的现有 Spring 项目中。缓存将在端点级别完成。为了做到这一点,使用 JAXB 从 XSD 生成的类需要实现Serializableinterface 和 overrideObjecttoString()方法。

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

4

6 回答 6

77

可序列化

xjc:serializable在自定义绑定文件中使用以将java.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>
于 2009-10-03T15:23:06.673 回答
8

这对我有用:

<?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>
于 2013-11-22T11:27:46.233 回答
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;
}
于 2012-12-09T12:14:46.117 回答
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>
于 2014-09-24T05:35:31.373 回答
1

为了获得接口 Serializable 将以下绑定信息添加到 xsd 文件:

<?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>

于 2016-09-01T15:23:50.677 回答
1

这是使用 CXF 3.1.10 的示例。记得包含编译组:'org.apache.cxf.xjc-utils',名称:'cxf-xjc-runtime',版本:'3.1.0'

<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>
于 2017-02-28T05:08:11.593 回答