我也有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>
一些試驗用不同的XSD配置後我發現,攔截器中的驗證在識別不合格屬性時存在問題。合格的人正在工作。 – steyze