Spring PayloadValidatingInterceptor评估XML(不正确)无效

时间:2013-01-17 15:34:15

标签: validation xsd spring-ws

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

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

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

有人知道WS中的验证与手动验证的行为有何不同?

1 个答案:

答案 0 :(得分:1)

我也遇到了PayloadValidatingInterceptor的问题。我根据我读到的here创建了一个简单的替代方案。有时拦截器不会按预期工作 - 基于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>