JAXB:如何自定义双字段的Xml序列化

Sam*_*erg 7 java jaxb xml-serialization

我有一个遗留类,有很多公共双字段.所有双字段都初始化Double.MAX_VALUE为表示它们是空的.(遗留序列化编码为忽略字段,如果字段等于则不进行序列化Double.MAX_VALUE).

我们现在尝试使用JAXB Marshaller将此类序列化为Xml.它工作正常,除了我们想要阻止为相等的字段生成Xml Double.MAX_VALUE.

我们没有使用单独的JAXB模式,只是用各种javax.xml.bind.annotation注释标记我们的类.如果使用模式,则可以添加<javaType>元素以指定自定义DataType转换器.有没有办法使用Annotations或以编程方式执行此操作?

在尝试下面推荐的方法后,我仍然无法XmlAdapter接受:

@XmlJavaTypeAdapters({
@XmlJavaTypeAdapter(value=EmptyDoubleValueHandler.class, type=Double.class), @XmlJavaTypeAdapter(value=EmptyDoubleValueHandler.class, type=double.class)})
package tta.penstock.data.iserver;

import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters;
Run Code Online (Sandbox Code Playgroud)

我的顶级课程是:tta.penstock.data.iserver.OrderBlotter,其中包含一个扩展的tta.penstock.data.iserver.OrderResponseWrappers列表com.eztech.OrderResponse.所有双字段都包含在com.eztech.OrderResponse.

我的单元测试代码执行以下操作:

JAXBContext context = JAXBContext.newInstance(new Class[] { OrderBlotter.class, OrderResponseWrapper.class, OrderResponse.class});

Marshaller marshaller = context.createMarshaller();
StringWriter stringWriter = new StringWriter();
marshaller.marshal(blotter, stringWriter);
System.out.println("result xml=\n" + stringWriter.toString());
Run Code Online (Sandbox Code Playgroud)

但双重值仍然没有得到处理XmlAdapter.我知道我遗漏了一些基本的东西,但我不确定它是什么.

bdo*_*han 13

您可以使用XmlAdapter:

XmlAdapter

package example;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class DoubleAdapter extends XmlAdapter<Double, Double>{

    @Override
    public Double unmarshal(Double v) throws Exception {
        return v;
    }

    @Override
    public Double marshal(Double v) throws Exception {
       if(Double.MAX_VALUE == v) {
           return null;
       } else {
           return v;
       }
    }

}
Run Code Online (Sandbox Code Playgroud)

模型对象

package example;

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement
public class Root {

    @XmlJavaTypeAdapter(DoubleAdapter.class)
    public Double maxDouble = Double.MAX_VALUE;

    @XmlJavaTypeAdapter(DoubleAdapter.class)
    public Double aDouble = 123d;

}
Run Code Online (Sandbox Code Playgroud)

演示代码

package example;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(new Root(), System.out);
    }

}
Run Code Online (Sandbox Code Playgroud)

UPDATE

StaxMan的建议很好.如果指定以下包级别注释,则可以避免单独注释所有Double属性

package-info.java

@XmlJavaTypeAdapters({
    @XmlJavaTypeAdapter(type=Double.class, value=DoubleAdapter.class)
})
package example;

import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters;
Run Code Online (Sandbox Code Playgroud)

  • 您还可以为包添加@XmlAdapter注释(package-info.java可以包含注释!) - 查看java文档.这可能使这种方法可行 (3认同)