【发布时间】:2015-09-08 20:47:59
【问题描述】:
我有一条 SOAP 消息,我将使用 CXF 3.0.3 中的 commonValidationFeature 对其进行验证。验证器工作,但它返回一个没有有意义的错误消息的肥皂错误。
我想捕获 ValidationException 并将其转换为带有状态错误代码中的错误的 SOAP 消息。稍后我将描述输出消息。
这里返回了soap错误:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:Fault>
<faultcode>soap:Server</faultcode>
<faultstring>Fault occurred while processing.</faultstring>
</soap:Fault>
</soap:Body>
</soap:Envelope>
验证错误应该是:
字段 [xyz] 无效,必须大于 0。(类似)
这里的字段是 XSD
<xs:element minOccurs="0" name="merchantId">
<xs:simpleType>
<xs:restriction base="xs:long">
<xs:minInclusive value="1" />
</xs:restriction>
</xs:simpleType>
</xs:element>
生成的java代码 @XmlElement(type = String.class) @XmlJavaTypeAdapter(Adapter2 .class) @DecimalMin("1") 受保护的长商家ID;
这里是我的端点中使用的验证
<jaxws:features>
<ref bean="commonValidationFeature" />
</jaxws:features>
<bean id="commonValidationFeature" class="org.apache.cxf.validation.BeanValidationFeature"/>
我试图创建我的拦截器来捕获错误,但是当我这样做时,我收到了用于解组的架构验证错误。类似于我上一个问题的消息:CXF SOAP JAX-WS WSS4JInInterceptor change namespace and cause Unmarshalling error
这是我的拦截器代码(测试代码)
public class ExceptionInterceptor extends AbstractSoapInterceptor {
ConstraintViolationExceptionMapper mapper = new ConstraintViolationExceptionMapper();
public ExceptionInterceptor() {
super(Phase.PRE_LOGICAL);
}
public void handleMessage(SoapMessage message) throws Fault {
Fault fault = (Fault) message.getContent(Exception.class);
Throwable ex = fault.getCause();
if (ex instanceof ConstraintViolationException) {
ConstraintViolationException e = (ConstraintViolationException) ex;
Response response = mapper.toResponse(e);
generateSoapFault(fault, e, response);
} else {
generateSoapFault(fault, null, null);
}
}
private void generateSoapFault(Fault fault, Exception e, Response response) {
fault.setFaultCode(createQName(response.getEntity().toString()));
}
private static QName createQName(String errorCode) {
return new QName("qname.com", errorCode);
}
}
我想得到的输出是这样的
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns6:newOutputResponse xmlns:ns6="http://demo.test" >
<asset>
...
</asset>
<status>
<type>ERROR</type>
<description>[merchantId] is invalid, must be geater than 0.</description>
</....
</ns6...>
....
只有在出现错误时才会添加标签。我该怎么做?
【问题讨论】:
-
Sebastien 运气好能找到您问题的答案吗?
-
是的,我有东西。感谢让我记得更新这个问题。我会尽快给出答案
标签: soap xsd cxf validation