问题描述
我想在一个现有的 Spring 项目中引入缓存,该项目使用 JAXB 来公开 WebServices.缓存将在端点级别完成.为了做到这一点,使用 JAXB 从 XSD 生成的类需要实现 Serializable
接口并覆盖 Object
的 toString()
方法.
I would like to introduce caching into an existing Spring project which uses JAXB to expose WebServices. Caching will be done on the level of end points. In order to do that classes generated from XSD using JAXB need to implement Serializable
interface and override Object
's toString()
method.
如何使用 XSD 指示 xjc 工具生成具有所需属性的源代码?
How to instruct the xjc tool using XSD to generate source with needed properties?
推荐答案
Serializable
在自定义绑定文件中使用 xjc:serializable
将 java.io.Serializable
接口与 serialVersionUID
一起添加到您的类中:
Serializable
Use xjc:serializable
in a custom bindings file to add the java.io.Serializable
interface to your classes along with a 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()
实现):
toString()
Use a superclass (see xjc:superClass
) from which all your bound classes will inherit. This class won’t be generated by xjc so you are free to create it as you please (here with a toString()
implementation):
<?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>
这篇关于如何使用 JAXB 从 xsd 生成实现 Serializable 接口的 Java 类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!