Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JAXB从'beforemashall(Marshaller)'方法返回什么?_Java_Jaxb - Fatal编程技术网

Java JAXB从'beforemashall(Marshaller)'方法返回什么?

Java JAXB从'beforemashall(Marshaller)'方法返回什么?,java,jaxb,Java,Jaxb,首先,我不是在说。 我说的是那些类定义的事件回调 有谁能告诉我,布尔beforemashall(Marshaller)方法应该返回什么 /** *这种方法的apidocs在哪里? *我该拿什么回来? */ 布尔型beforemashall(Marshaller-Marshaller); 我的意思是,无论如何,使用这种方法将JPA的Long@Id转换为JAXB的String@XmlID,使用JAXB-RI和而不使用MOXy [已编辑] 尽管如此,void版本似乎仍在运行。这只是一个文档问题吗?简

首先,我不是在说。 我说的是那些
类定义的
事件回调

有谁能告诉我,布尔beforemashall(Marshaller)
方法应该返回什么

/**
*这种方法的apidocs在哪里?
*我该拿什么回来?
*/
布尔型beforemashall(Marshaller-Marshaller);
我的意思是,无论如何,使用这种方法将
JPA的Long@Id转换为JAXB的String@XmlID
,使用JAXB-RI而不使用MOXy

[已编辑]
尽管如此,
void
版本似乎仍在运行。这只是一个文档问题吗?

简短回答

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

长答案

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

您可以使用,因为它不受使用
@XmlID
注释的字段/属性为
String
类型的限制

有JAXB-RI,没有MOXy

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

IDAdapter

XmlAdapter
Long
值转换为
字符串
值,以满足
@XmlID
注释的要求

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);
    }

}
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>

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