如何以编程方式更改默认的JAXB日期序列化? [英] How to change programmatically the default JAXB date serialization?

查看:198
本文介绍了如何以编程方式更改默认的JAXB日期序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法改变jaxb序列化/反序列化类型的默认方式,在我的情况下为日期,而不通过注释和/或通过xml jaxb绑定指定它,如下所述
http://jaxb.java.net/guide/Using_different_datatypes.html

Is there a way to change the default way jaxb serialize/deserialize types, dates in my case, without specifying it through annotation and/or through xml jaxb binding as mentioned here http://jaxb.java.net/guide/Using_different_datatypes.html

我基本上喜欢做类似的事情:

I'd basically like to do something like:

    JAXBContext jaxbContext = ...;
    Marshaller marshaller = jaxbContext.createMarshaller().setAdapter(new DateAdapter(dateFormat));

使用预先配置的JaxBContext或Marshaller / Unmarshaller以自定义方式序列化/反序列化日期..

To have a preconfigured JaxBContext or Marshaller/Unmarshaller that serialize/deserialize dates in a customized way..

找不到任何显示如何通过注释或静态使用xml绑定文件的资源..
谢谢!

Couldn't find any resource that shows how to do expect through annotations or statically with the xml binding file.. Thanks!

推荐答案

这并不是您正在寻找的内容,但它可以单独为每个日期字段添加注释。您可以在包级别设置XmlJavaTypeAdapter,以便包中对Date的每个引用都将使用它。如果您的对象位于com.example包中,则应使用以下内容向其添加package-info.java文件:

This isn't exactly what you're looking for but it beats annotating every Date field individually. You can set a XmlJavaTypeAdapter at the package level so that every reference to Date within your package will use it. If your objects are in the com.example package, you should add a package-info.java file to it with the following contents:

@XmlJavaTypeAdapter(value=MyCustomDateAdapter.class,type=Date.class)
package com.example;

这篇关于如何以编程方式更改默认的JAXB日期序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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