Mat*_*ngo 14 web-services cxf jax-ws jaxb xml-validation
我正在开发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>
Run Code Online (Sandbox Code Playgroud)
我已经探索过在服务器上使用拦截器(例如BareInInterceptor),并以某种方式捕获SAXParseExceptions来包装它们并将它们发送到客户端.这种方法看起来有点复杂,但如果XML无效,我需要以某种方式为客户端提供一个行号.我应该使用拦截器来揭露异常吗?
我对这个技术堆栈并不是很有经验,只是进入Web服务 - 你们给我的任何指针都会非常感激.
Sam*_*Sam 27
您可以使用自定义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()+"]");
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果将端点配置为使用此处理程序:
<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>
Run Code Online (Sandbox Code Playgroud)
然后,您将获得如下所示的SOAP错误:
<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>
Run Code Online (Sandbox Code Playgroud)
jaxb-validation-event-handler属性最近才被添加到CXF,因此您需要确保使用最新版本 - 我使用2.2.5进行了测试.
| 归档时间: |
|
| 查看次数: |
18819 次 |
| 最近记录: |