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>