gpt4 book ai didi

java - SAAJ 与 MTOM + XOP 兼容吗?

转载 作者:行者123 更新时间:2023-11-30 08:34:50 25 4
gpt4 key购买 nike

我正在尝试使用 MTOM + XOP 在 Spring Web 服务中返回一个 byte[] 但是当编码结果时它会抛出一个带有消息的 SAXParseException:

cvc-type.3.1.2: 元素'dataHandler'是一个简单类型,所以它必须没有元素信息项[children]。

似乎在写入 XOP 标记后, validator 遇到它而不是它期望的 byte[] 内容,并抛出一个 FATAL 级别的 SAXParseException 来停止进程。

我正在使用:

  • JDK 1.7
  • SAAJ 1.3
  • SOAP 1.1
  • Spring 4.1.4.RELEASE
  • Spring WS 2.2.0.RELEASE

提前致谢

最佳答案

针对我的情况,我找到了两个解决方法:

  1. 没有将 xmlschema 设置为编码器。

  2. ValidationEventHandler 设置为跳过 XOP 失败的编码器。

这是将 ValidationEventHandler 设置为跳过 XOP 失败的编码器的示例:

抽象父类(super class):

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

对于编码器,我正在搜索标识大小写的条件,因为 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()));
}
}

关于java - SAAJ 与 MTOM + XOP 兼容吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38596474/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com