【问题标题】:How CXF SOAP convert XSD ContraintException to meaningful message?CXF SOAP 如何将 XSD ContraintException 转换为有意义的消息?
【发布时间】: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


【解决方案1】:

我找到了一种方法。我们必须创建一个消息响应并使用 ContrainsViolation 错误填充此消息。这里是一个例子

public static StatusType createStatusType(Set<ConstraintViolation<?>> violations, Throwable ex, Fault fault){
    StatusType status;

    if(violations!=null) {
        status = new StatusType();
        status.setStatus(StatusFormat.ERROR);

        for (ConstraintViolation<?> violation : violations) {
            DetailType detailType = new DetailType();
            detailType.setCode(ErrorMessageConstant.PARAMETER_VALIDATION_ERR_CODE);
            detailType.setDescription(violation.getPropertyPath().toString() + " : " + violation.getMessage());

            status.getDetail().add(detailType);
        }
    } else if(ex instanceof javax.xml.bind.UnmarshalException){
        status = new StatusType();
        status.setStatus(StatusFormat.ERROR);

        DetailType detailType = new DetailType();
        detailType.setCode(ErrorMessageConstant.PARAMETER_VALIDATION_ERR_CODE);
        detailType.setDescription(((UnmarshalException) ex).getLinkedException().getMessage());

        status.getDetail().add(detailType);
    } else {
        if(ex==null) {
            status = MessageHelper.buildStatusType(StatusFormat.ERROR, ErrorMessageConstant.INTERNAL_SERVER_ERR_CODE, fault.getMessage());
        } else {
            status = MessageHelper.buildStatusType(StatusFormat.ERROR, ErrorMessageConstant.INTERNAL_SERVER_ERR_CODE, ex.toString());
        }
    }

    return status;
}

【讨论】:

    猜你喜欢
    • 2011-07-07
    • 1970-01-01
    • 1970-01-01
    • 2017-07-11
    • 2014-07-01
    • 2012-04-20
    • 1970-01-01
    • 2019-10-16
    • 1970-01-01
    相关资源
    最近更新 更多