問題描述
我想在一個現有的 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 類?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!