对于我的情况,我找到了两种解决方法:
不将xmlschema设置为marshaller。
为marshaller设置一个跳过XOP故障的ValidationEventHandler
。
以下是设置跳过XOP故障的ValidationEventHandler
的示例:
抽象超类:
import javax.xml.bind.ValidationEvent;
import javax.xml.bind.ValidationEventHandler;
abstract class XopAwareValidationEventHandler implements ValidationEventHandler {
private static final String CVC_TYPE_3_1_2 = "cvc-type.3.1.2";
private ValidationEventHandler realHandler;
XopAwareValidationEventHandler(final ValidationEventHandler handler) {
this.setRealHandler(handler);
}
@Override
public boolean handleEvent(final ValidationEvent event) {
final boolean result = this.getRealHandler().handleEvent(event);
if (!result) {
if (event.getMessage() != null && event.getMessage().startsWith(CVC_TYPE_3_1_2)) {
return this.isXopEvent(event);
}
}
return result;
}
abstract boolean isXopEvent(ValidationEvent validationEvent);
private ValidationEventHandler getRealHandler() {
return realHandler;
}
private void setRealHandler(final ValidationEventHandler realHandler) {
this.realHandler = realHandler;
}
}
用于反序列化的具体类:
import javax.xml.bind.ValidationEvent;
import javax.xml.bind.ValidationEventHandler;
class XopAwareUnmarshallingValidationEventHandler extends XopAwareValidationEventHandler {
private static final String XOP_INCLUDE = "xop:Include";
XopAwareUnmarshallingValidationEventHandler(final ValidationEventHandler handler) {
super(handler);
}
@Override
boolean isXopEvent(final ValidationEvent validationEvent) {
final ValidationEventLocator locator = validationEvent.getLocator();
return locator != null && locator.getNode() != null &&
locator.getNode().getFirstChild() != null &&
XOP_INCLUDE.equals(locator.getNode().getFirstChild().getNodeName());
}
}
对于marshaller来说,我正在寻找能够识别情况的条件,因为ValidationEventLocator
只设置了对象,它可能是任何东西。
import javax.xml.bind.ValidationEvent;
import javax.xml.bind.ValidationEventHandler;
import javax.xml.bind.ValidationEventLocator;
public class XopAwareMarshallingValidationEventHandler extends XopAwareValidationEventHandler {
public XopAwareMarshallingValidationEventHandler(final ValidationEventHandler handler) {
super(handler);
}
boolean isXopEvent(final ValidationEvent validationEvent) {
final ValidationEventLocator locator = validationEvent.getLocator();
return locator != null && locator.getNode() == null;
}
}
org.springframework.oxm.jaxb.Jaxb2Marshaller
是一个子类,它可以激活 MTOM 并添加事件处理程序:
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import XopAwareMarshallingValidationEventHandler;
import XopAwareUnmarshallingValidationEventHandler;
public class XopAwareJaxb2Marshaller extends Jaxb2Marshaller {
public XopAwareJaxb2Marshaller() {
this.setMtomEnabled(true);
}
protected void initJaxbMarshaller(final Marshaller marshaller) throws JAXBException {
super.initJaxbMarshaller(marshaller);
marshaller.setEventHandler(new XopAwareMarshallingValidationEventHandler(marshaller.getEventHandler()));
}
protected void initJaxbUnmarshaller(final Unmarshaller unmarshaller) throws JAXBException {
super.initJaxbUnmarshaller(unmarshaller);
unmarshaller.setEventHandler(new XopAwareUnmarshallingValidationEventHandler(unmarshaller
.getEventHandler()));
}
}