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

How to generate a Java class which implements Serializable interface from xsd using JAXB?(如何使用 JAXB 从 xsd 生成实现 Serializable 接口的 Java 类?)
本文介绍了如何使用 JAXB 从 xsd 生成实现 Serializable 接口的 Java 类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想在一个现有的 Spring 项目中引入缓存,该项目使用 JAXB 来公开 WebServices.缓存将在端点级别完成.为了做到这一点,使用 JAXB 从 XSD 生成的类需要实现 Serializable 接口并覆盖 ObjecttoString() 方法.

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:serializablejava.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 类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)