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

问题描述 投票:0回答:7

我想将缓存引入到现有的 Spring 项目中,该项目使用 JAXB 来公开 Web 服务。缓存将在端点级别完成。为了做到这一点,使用 JAXB 从 XSD 生成的类需要实现

Serializable
接口并覆盖
Object
toString()
方法。

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

java xsd jaxb xjc
7个回答
80
投票

可序列化

在自定义绑定文件中使用

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>

10
投票

这对我有用:

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

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;
}

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>

1
投票

为了获取 Serialized 接口,请将以下绑定信息添加到 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>


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>

0
投票

基于 jaxb2-maven-plugin:2.5.0 插件对我的项目有用:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxb2-maven-plugin</artifactId>
    <version>2.5.0</version>
    <executions>
        <execution>
          ...
        </execution>
        <execution>
            <id>schema_variations-scopes</id>
            <goals>
                <goal>xjc</goal>
            </goals>
            <configuration>
                ...
                <xjbSources>
                    <xjbSource>src/main/resources/xjc/bindings.xjb</xjbSource>
                </xjbSources>
            </configuration>
        </execution>
    </executions>
</plugin>

并将绑定.xml文件放置在路径下:

src/main/resources/xjc/bindings.xjb

<jxb:bindings version="1.0"
              xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
              xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
              xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 
    <jxb:globalBindings generateIsSetMethod="true">
        <xjc:serializable uid="12343"/>
    </jxb:globalBindings>
</jxb:bindings>

© www.soinside.com 2019 - 2024. All rights reserved.