我也遇到了一些问题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>