我正在以下列方式使用cxf创建一个web服务:
<cxf:cxfEndpoint id=XXXEndpoint"
serviceClass="com.Sth"
address="${webservices.url}/XXX"
wsdlURL="${wsdl.address}/services/XXX.wsdl"
endpointName="m:XXXPort"
serviceName="m:XXXService"
xmlns:m="http://com.sth/XXX">
<cxf:properties>
<entry key="schema-validation-enabled" value="true" />
</cxf:properties>
</cxf:cxfEndpoint>
它完美运行,还添加了模式验证。我无法添加自定义验证处理程序。我怎么能这样做?
答案 0 :(得分:4)
我不确定自定义验证处理程序的含义。
如果要更改验证错误处理,可以创建实现javax.xml.bind.ValidationEventHandler
的类例如,我使用这种方法来防止JAXB在第一次遇到的错误时抛出异常。我的自定义事件处理程序收集了所有非致命验证错误,并在验证整个传入消息后抛出了相应的异常。
Sample use of 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”字符串,则会跳过您的处理程序,或者是以下错误之一:
(坦率地说,这似乎是CXF的一个肮脏的黑客,如果它对你来说是一个问题,我会为CXF团队制造一个错误。)
如果您想要更多错误处理自定义,您应该考虑编写自己的Interceptor。执行此类验证的最佳阶段可能是(PRE / USER / POST)_LOGICAL中的一个。