【问题标题】:Apache CXF : get XML request causing FaultApache CXF:获取导致故障的 XML 请求
【发布时间】:2016-11-24 14:45:01
【问题描述】:

我有一个需要某种请求格式的网络服务。 当我发送无效的请求格式时,出现以下错误:

<faultcode>soap:Client</faultcode>
<faultstring>Unmarshalling Error: unexpected element (uri:"", local:"aaaa"). Expected elements are &lt;{}xxx>,&lt;{}yyy></faultstring>

发生这种情况时,我需要发送电子邮件。在这封电子邮件中,我需要传入的请求(我发送到 Web 服务的 XML)。

我尝试实现一个 CXF 拦截器:

public class ExceptionInterceptor extends AbstractSoapInterceptor {

    /**
     * Logger
     */
    private final static Logger LOGGER = LoggerFactory.getLogger(ExceptionInterceptor.class);

    /**
     * Constructeur
     */
    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 UnmarshalException) {
            LOGGER.error("Error in incoming message", ex);

            // TODO : send email
        }
    }
}

但是...我怎样才能在这里获得我发送到网络服务的原始消息? Apache CXF 拦截器的文档不多。 :(

提前致谢!

赫克

【问题讨论】:

    标签: java xml apache web-services cxf


    【解决方案1】:

    您可以使用默认的 CXF 记录器 LoggingInInterceptorLoggingOutInterceptor 记录肥皂消息,或者使用自定义拦截器来提取有效负载

    使用 CXF 记录器

    // output log using log4j
    //LogUtils.setLoggerClass(org.apache.cxf.common.logging.Log4jLogger.class);
    yourService = new YourService(wsdlURL, SERVICE_NAME);
    port = yourService.getServicePort(); 
    
    Client client = ClientProxy.getClient(port);
    client.getInInterceptors().add(new LoggingInInterceptor());
    client.getOutInterceptors().add(new LoggingOutInterceptor());
    

    消息将通过控制台写入。您可以使用 log4j 或配置 java.util.logging。查看更多示例here

    自定义拦截器

    public class MyInterceptor extends AbstractPhaseInterceptor<Message> {
        public MyInterceptor () {
            super(Phase.RECEIVE);
        }
    
        public void handleMessage(Message message) {
            //Get the message body into payload[] and set a new non-consumed  inputStream into Message
            InputStream in = message.getContent(InputStream.class);
            byte payload[] = IOUtils.readBytesFromStream(in); 
            //log payload...
            ByteArrayInputStream bin = new ByteArrayInputStream(payload);
            message.setContent(InputStream.class, bin);
        }
    
        public void handleFault(Message messageParam) {
            //Invoked when interceptor fails
        }
    }
    

    以编程方式添加拦截器

     WebClient.getConfig(client).getOutInterceptors().add(new MyInterceptor());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-24
      • 2017-10-13
      • 1970-01-01
      • 2013-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多