使用 CXF Web 服务进行服务器端 XML 验证 [英] Server-Side XML Validation with CXF Webservice

查看:25
本文介绍了使用 CXF Web 服务进行服务器端 XML 验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 Apache CXF 网络服务(使用 JAX-WS,通过 SOAP).服务本身非常简单:接收请求,将请求插入数据库,并返回插入是否成功.我想依靠 XML 验证来对请求实施一些约束.

I'm working on an Apache CXF webservice (using JAX-WS, over SOAP). The service itself is pretty simple: receive a request, insert the request into a database, and return whether the insert was successful. I'd like to rely on XML validation to enforce a number of constraints on the request.

所以,我的问题.如何将详细的验证错误返回给我的服务的客户端?我已通过配置端点在服务器端启用验证.

So, my question. How do I return detailed validation errors to a client of my service? I've turned validation on server-side by configuring my endpoint.

<jaxws:endpoint id="someEndpoint" implementor="#someImpl" address="/impl">
    <jaxws:properties>
        <!-- This entry should- ideally- enable JAXB validation
        on the server-side of our web service. -->
        <entry key="schema-validation-enabled" value="true" />
    </jaxws:properties>
</jaxws:endpoint>

我探索了在服务器上使用拦截器(例如 BareInInterceptor),并以某种方式捕获 SAXParseExceptions 来包装它们并将它们发送到客户端.这种方法看起来有点复杂,但如果他们的 XML 无效,我需要以某种方式给客户一个行号.我应该使用拦截器来暴露异常吗?

I've explored using interceptors (eg BareInInterceptor) on the server, and somehow catching SAXParseExceptions to wrap them and send them along to the client. This approach seems a bit complicated, but I need to somehow give clients a line number if their XML is invalid. Should I go with interceptors to expose the exceptions?

我对这个技术栈的经验不是很丰富,刚开始接触网络服务——你们能给我的任何指点都将不胜感激.

I'm not very experienced with this technology stack, and just getting in to web services- any pointers you guys can give me would be really appreciated.

推荐答案

您可以使用自定义 ValidationEventHandler 覆盖验证错误消息,插入行号:

You can override validation error messages, inserting a line number, by using a custom ValidationEventHandler:

package example;

import javax.xml.bind.ValidationEvent;
import javax.xml.bind.helpers.DefaultValidationEventHandler;

public class MyValidationEventHandler extends DefaultValidationEventHandler {    
    @Override
    public boolean handleEvent(ValidationEvent event) {
        if (event.getSeverity() == ValidationEvent.WARNING) {
            return super.handleEvent(event);
        } else {
            throw new RuntimeException(event.getMessage()
                + " [line:"+event.getLocator().getLineNumber()+"]");
        }
    }
}

如果您将端点配置为使用此处理程序:

If you configure your endpoint to use this handler:

<jaxws:endpoint id="someEndpoint" implementor="#someImpl" address="/impl">
    <jaxws:properties>
        <entry key="schema-validation-enabled" value="true" />
        <entry key="jaxb-validation-event-handler">
            <bean class="example.MyValidationEventHandler" />
        </entry>
    </jaxws:properties>
</jaxws:endpoint>

然后你会得到如下所示的 SOAP 错误:

Then you will get SOAP faults that look like this:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <soap:Fault>
            <faultcode>soap:Client</faultcode>
            <faultstring>Unmarshalling Error: Not a number: xyz [line: 6]</faultstring>
        </soap:Fault>
    </soap:Body>
</soap:Envelope>

jaxb-validation-event-handler 属性最近才添加到 CXF,因此您需要确保使用的是最新版本 - 我在 2.2.5 中对此进行了测试.

The jaxb-validation-event-handler property was only added to CXF pretty recently, so you need to make sure you're using the latest version - I tested this with 2.2.5.

这篇关于使用 CXF Web 服务进行服务器端 XML 验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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