我不确定自定义验证处理程序是什么意思。
如果要更改验证错误处理,可以创建实现javax.xml.bind.ValidationEventHandler的类
例如,我使用这种方法来防止 JAXB 在第一次遇到错误时抛出异常。我的自定义事件处理程序收集了所有非致命的验证错误,并在验证整个传入消息后引发了适当的异常。
ValidationEventHandler 的示例使用
为了使用您的自定义验证事件处理程序,您应该添加 jaxb-validation-event-handler 属性:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd">
<jaxws:endpoint id="HTTPEndpoint"
implementor="org.dpytel.servicemix.cxf.wsdlfirst.PersonImpl" address="/PersonService"
wsdlLocation="wsdl/person.wsdl" endpointName="e:soap" serviceName="s:PersonService"
xmlns:e="http://servicemix.apache.org/samples/wsdl-first" xmlns:s="http://servicemix.apache.org/samples/wsdl-first">
<jaxws:properties>
<entry key="schema-validation-enabled" value="true" />
<entry key="jaxb-validation-event-handler">
<bean class="org.dpytel.servicemix.cxf.wsdlfirst.MyCustomHandler"></bean>
</entry>
</jaxws:properties>
</jaxws:endpoint>
</beans>
Camel CXF 端点配置:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://camel.apache.org/schema/cxf"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://camel.apache.org/schema/cxf
http://camel.apache.org/schema/cxf/camel-cxf.xsd">
<cxf:cxfEndpoint id="personEndpoint" address="/person"
serviceClass="org.apache.servicemix.samples.wsdl_first.Person"
wsdlURL="wsdl/person.wsdl">
<cxf:properties>
<entry key="schema-validation-enabled" value="true" />
<entry key="jaxb-validation-event-handler">
<bean class="org.dpytel.servicemix.camel.MyCustomHandler" />
</entry>
</cxf:properties>
</cxf:cxfEndpoint>
</beans>
禁用验证错误并仅记录验证消息的示例处理程序:
import java.util.logging.Logger;
import javax.xml.bind.ValidationEvent;
import javax.xml.bind.ValidationEventHandler;
public class MyCustomHandler implements ValidationEventHandler {
private Logger logger = Logger.getLogger(this.getClass().getCanonicalName());
public boolean handleEvent(ValidationEvent event) {
logger.severe("Error: " + event.getMessage());
return true;
}
}
请注意,一些验证错误会导致 CXF 跳过调用您的处理程序(请参阅DataReaderImpl.WSUIDValidationHandler.handleEvent(...)的详细信息)。如果错误消息包含 ":Id" 字符串,或者是以下错误之一,您的处理程序将被跳过:
- cvc-type.3.1.1
- cvc-type.3.2.2
- cvc-complex-type.3.1.1
- cvc-complex-type.3.2.2
(坦率地说,这在 CXF 中似乎是一个肮脏的 hack,如果这对你来说是个问题,我会为 CXF 团队创建一个错误)。
如果您想要更多的错误处理自定义,您可能应该考虑编写自己的Interceptor。执行此类验证的最佳阶段可能是 (PRE/USER/POST)_LOGICAL 之一。