CXF验证自定义处理程序 [英] CXF validation custom handler

查看:106
本文介绍了CXF验证自定义处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过以下方式使用cxf创建Web服务:

I'm creating a webservice with cxf in the following way:

<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>

它运行良好,还添加了模式验证。我无法添加自定义验证处理程序。该怎么办?

it works perfectly, also added schema validation. I cannot add a custom validation handler. How can I do that?

推荐答案

我不确定自定义验证处理程序的含义。

I'm not sure what you mean by custom validation handler.

如果要更改验证错误处理,则可以创建实现 javax.xml.bind.ValidationEventHandler

If you want to change validation error handling you can create class implementing javax.xml.bind.ValidationEventHandler

例如,我使用了这个防止JAXB在遇到的第一个错误上引发异常的方法。我的自定义事件处理程序收集了所有非致命的验证错误,并在验证了整个传入消息后抛出了适当的异常。

For instance I used this approach to prevent JAXB from throwing exception on the first encountered error. My custom event handler collected all non-fatal validation errors and thrown appropriate exception after validating whole incoming message.

使用ValidationEventHandler的示例

要使用自定义验证事件处理程序,您应该添加jaxb -validation-event-handler属性:

In order to use your custom validation event handler you should add jaxb-validation-event-handler property:

<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>

骆驼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>

禁用验证错误并仅记录验证消息的示例处理程序:

Example handler that disables validation error and just logs the validation message:

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字符串,或者是以下错误之一,则将跳过处理程序:

Please note that some validation errors will cause CXF to skip calling your handler (See details of DataReaderImpl.WSUIDValidationHandler.handleEvent(...)). Your handler will be skipped if error message contains ":Id" string, or is one of the following errors:


  • cvc-type.3.1 .1

  • cvc-type.3.2.2

  • cvc-complex-type.3.1.1

  • cvc-complex-type.3.2.2

  • cvc-type.3.1.1
  • cvc-type.3.2.2
  • cvc-complex-type.3.1.1
  • cvc-complex-type.3.2.2

(坦率地说,这似乎是CXF中的肮脏技巧,如果它是您会为CXF团队创建一个错误)。

(frankly it seems like a dirty hack in CXF and if it is a problem for you I would create a bug for CXF team).

如果您想进行更多错误处理自定义,则应该考虑编写自己的拦截器。进行此验证的最佳阶段可能是(PRE / USER / POST)_LOGICAL之一。

If you want more error handling customization you should probably consider writing your own Interceptor. Probably the best phase to perform such validation would be one of (PRE/USER/POST)_LOGICAL.

这篇关于CXF验证自定义处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆