JAXB应该从`beforeMarshal(Marshaller)方法返回什么? [英] JAXB what should be returned from `beforeMarshal(Marshaller)` method?

查看:255
本文介绍了JAXB应该从`beforeMarshal(Marshaller)方法返回什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我不是在谈论的Marshaller#监听
我在谈论那些类定义的事件回调。

First of all, I'm not talking about Marshaller#Listener. I'm talking about those class defined event callbacks.

任何人都可以告诉我应该是什么从 boolean beforeMarshal(Marshaller)方法返回?

Can anybody tell me what should be returned from boolean beforeMarshal(Marshaller) method?

/**
 * Where is apidocs for this method?
 * What should I return for this?
 */
boolean beforeMarshal(Marshaller marshaller);

我的意思是,无论如何,使用此方法转换 JPA的Long @Id到JAXB的String @XmlID 使用JAXB-RI 使用没有MOXy

I mean, anyway, to use this method for converting JPA's Long @Id to JAXB's String @XmlID with JAXB-RI and without MOXy.

[已编辑]
a void 版本似乎正常工作。这只是一个文档问题?

[edited] a void version seems working though. is this just an documentation problem?

推荐答案

简答

boolean 返回类型是文档错误。返回类型应为 void

The boolean return type is a documentation error. The return type should be void.

长答案


我的意思是,无论如何,使用这种方法将JPA的Long @Id转换为
JAXB的String @XmlID

I mean, anyway, to use this method for converting JPA's Long @Id to JAXB's String @XmlID

您可以使用 EclipseLink JAXB(MOXy) ) ,因为它没有使用 @XmlID 注释的字段/属性的类型 String

You could use EclipseLink JAXB (MOXy) as it does not have the restriction that a field/property annotated with @XmlID be of type String.


使用JAXB-RI且没有MOXy。

with JAXB-RI and without MOXy.

您可以使用 XmlAdapter 来映射支持您的用例:

You could use an XmlAdapter to map support your use case:

IDAdapter

XmlAdapter 转换 Long 值为 String 值,以满足 @XmlID 注释的要求。

This XmlAdapter converts the Long value to a String value to meet the requirements of the @XmlID annotation.

package forum9629948;

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

public class IDAdapter extends XmlAdapter<String, Long> {

    @Override
    public Long unmarshal(String string) throws Exception {
        return DatatypeConverter.parseLong(string);
    }

    @Override
    public String marshal(Long value) throws Exception {
        return DatatypeConverter.printLong(value);
    }

}

B

@XmlJavaTypeAdapter 注释用于指定 XmlAdapter

package forum9629948;

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

@XmlAccessorType(XmlAccessType.FIELD)
public class B {

    @XmlAttribute
    @XmlID
    @XmlJavaTypeAdapter(IDAdapter.class)
    private Long id;

}

A

package forum9629948;

import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class A {

    private B b;
    private C c;

}

C

package forum9629948;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)public class C {

    @XmlAttribute
    @XmlIDREF
    private B b;

}

演示

package forum9629948;

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(A.class);

        File xml = new File("src/forum9629948/input.xml");
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        A a = (A) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(a, System.out);
    }

}

输入/输出

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<a>
    <b id="123"/>
    <c b="123"/>
</a>

这篇关于JAXB应该从`beforeMarshal(Marshaller)方法返回什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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