2

我正在使用 Sping-WS (2.1) 开发一个 Spring 项目。

使用 PayloadValidatingInterceptor 我们通过 Spring Mock 或soapUI 发送的消息被评为无效。

我们多次检查了 XSD,我们确信数据和架构是正确的。我使用 springframework.xml-validator 引擎和 javax.xml.validation 类在我们的 SpringJUnit4ClassRunner 内部实现了两个测试用例,以根据模式验证 WS 外部的有效负载。在那里它们是有效的。

有谁知道为什么 WS 内部的验证行为与手动验证不同的原因?

4

1 回答 1

1

我也遇到了一些问题PayloadValidatingInterceptor。根据我在此处阅读的内容,我创建了一个简单的替代方案。有时拦截器不会按预期工作 - 使其StringReader基于而不是String基于解决我的问题。也许这也有助于解决您的问题!


要详细说明那里给出的解决方案,您可以用这样的代理类替换拦截器:

import java.io.StringReader;
import java.io.StringWriter;

import org.springframework.ws.client.WebServiceTransformerException;
import org.springframework.ws.soap.server.endpoint.interceptor.PayloadValidatingInterceptor;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.springframework.ws.WebServiceMessage;

public class PayloadValidatingInterceptorWithSourceFix extends
        PayloadValidatingInterceptor {

    @Override
    protected Source getValidationRequestSource(WebServiceMessage request) {
        return transformSourceToStreamSourceWithStringReader(request
                .getPayloadSource());
    }

    @Override
    protected Source getValidationResponseSource(WebServiceMessage response) {
        return transformSourceToStreamSourceWithStringReader(response
                .getPayloadSource());
    }

    Source transformSourceToStreamSourceWithStringReader(
            Source notValidatableSource) {
        final Source source;
        try {
            Transformer transformer = TransformerFactory.newInstance()
                    .newTransformer();

            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
                    "yes");
            transformer.setOutputProperty(OutputKeys.INDENT, "no");
            StringWriter writer = new StringWriter();
            transformer.transform(notValidatableSource,
                    new StreamResult(writer));

            String transformed = writer.toString();
            StringReader reader = new StringReader(transformed);
            source = new StreamSource(reader);

        } catch (TransformerException transformerException) {
            throw new WebServiceTransformerException(
                    "Could not convert the source to a StreamSource with a StringReader",
                    transformerException);
        }

        return source;
    }
}

并像使用它一样

<bean id="payloadValidatingInterceptorWithSourceFix"
    class="path.to.your.PayloadValidatingInterceptorWithSourceFix">
    <property name="schema"
        value="file:WebContent/WEB-INF/schemas/account-balance-service.xsd" />
    <property name="validateRequest" value="true" />
    <property name="validateResponse" value="true" />
</bean>
于 2013-01-23T09:43:06.277 回答