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

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

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