How to generate a Java class which implements Serializable interface from xsd using JAXB?(如何使用 JAXB 从 xsd 生成实现 Serializable 接口的 Java 类?)
问题描述
我想在一个现有的 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 类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 JAXB 从 xsd 生成实现 Serializable 接口的


基础教程推荐
- 多个组件的复杂布局 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 从 python 访问 JVM 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 大摇大摆的枚举 2022-01-01