使用 CXF Web 服务进行服务器端 XML 验证
我正在开发 Apache CXF Web 服务(使用 JAX-WS,通过 SOAP)。该服务本身非常简单:接收请求,将请求插入数据库,然后返回插入是否成功。我想依靠 XML 验证来对请求实施一些约束。
那么,我的问题。如何向我的服务客户返回详细的验证错误?我通过配置端点在服务器端启用了验证。
<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'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>
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用自定义 ValidationEventHandler 来覆盖验证错误消息,插入行号:
如果您将端点配置为使用此处理程序:
那么您将得到如下所示的 SOAP 错误:
jaxb-validation-event-handler 属性仅是最近添加到 CXF,因此您需要确保使用的是最新版本 - 我使用 2.2.5 进行了测试。
You can override validation error messages, inserting a line number, by using a custom ValidationEventHandler:
If you configure your endpoint to use this handler:
Then you will get SOAP faults that look like this:
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.