我想向大家介绍缓存成采用JAXB暴露Web服务现有的Spring项目。 缓存将在终点的水平来实现。 为了做到这一点使用JAXB从XSD生成的类需要实现Serializable
的界面和重写Object
的toString()
方法。
如何使用XSD生成所需要的属性源指示XJC工具?
我想向大家介绍缓存成采用JAXB暴露Web服务现有的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>
使用超(见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>
这为我工作:
<?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()
方法- JAXB2基础插件 。 这样看起来更好,因为不使用反射。 例如如何与下面行家做。
<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文件序列化加:
<?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>