如何使用JAXB生成一个从xsd实现Serializable接口的Java类? [英] How to generate a Java class which implements Serializable interface from xsd using JAXB?

查看:130
本文介绍了如何使用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?

推荐答案

可序列化



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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆