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

我想将caching引入到现有的使用JAXB公开Web服务的Spring项目中。 高速caching将在终点级别完成。 为了做到从XSD使用JAXB生成的类需要实现Serializable接口并重写ObjecttoString()方法。

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

序列化

在自定义绑定文件中使用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()

使用所有绑定类将从其inheritance的超类(请参阅xjc:superClass )。 这个类不会被xjc生成,所以你可以随意创build它(这里有一个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 Basics Plugins 。 这种方式看起来更好,因为不使用reflection。 示例如何使用下面的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> 

为了获得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>

这里是一个使用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>