JAXBを使用して、XSDからSerializableインタフェースを実装するJavaクラスを生成するには?
質問
私は、Webサービスを公開するためにJAXBを使用して、既存のSpringプロジェクトにキャッシングを紹介したいと思います。キャッシングは、エンドポイントのレベルで行われます。 JAXBを使用してXSDから生成されたクラスはSerializable
インターフェイスを実装し、Object
のtoString()
メソッドをオーバーライドする必要があることを行うために。
どのように必要なプロパティを持つソースを生成するために、XSDを使用してXJCツールを指示するには?
解決
直列化
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
を参照)スーパークラスを使用します。あなたは(ここでtoString()
実装と)下さいますよう、それを作成するのは自由ですので、このクラスは、XJCによって生成されることはありません。
<?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>
他のヒント
これは私のために働いてます:
<?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>
それがお役に立てば幸いです。
もう一つの方法は、toString()
メソッドを生成します。リフレクションを使用していないので、この方法が良く見えます。以下の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;
}
私はこのコードを使用して私のための仕事です。
<?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>
XSDファイルにバインディング情報を、以下のSerializableのアドオンをインターフェース得るために:
<?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>
そして、ここでは、CXF 3.1.10を使用したサンプルです。 'org.apache.cxf.xjc-utilsの' 名: 'CXF-XJCランタイム'、バージョン '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>