首页 > 解决方案 > axis2客户端记录请求响应spring boot

问题描述

我们如何记录从 Spring 引导服务类调用的Axis2 SOAP 请求和响应。Spring logback looging 不记录客户端请求和响应。

标签: spring-bootsoapaxis2

解决方案


我不知道从 Spring 调用是否会改变任何内容,但您可以将 hadlers 添加到您的axis2客户端。Axis2 分阶段处理消息。您可以获得所有这些阶段并将您的自定义处理程序添加到您想要的任何阶段。这样的处理程序可以用消息做所有事情,包括记录整个内容或修改它。

// get AxisConfiguration from client
AxisConfiguration axisConf = stub._getServiceClient().getAxisConfiguration();

// create your custom phase
Phase phase = new Phase(OUT_LOGGER);

// add your custom handler to your custom phase
phase.addHandler(new MessageContentLoggerHandler());

// get all phases processed when message is sent away 
// and add your custom phase
axisConf.getOutFlowPhases().add(phase);

// get all phases for incoming messages 
//and add handler to Security phase 
// (that's where security headers are added to SOAP message)
List<Phase> phasesIn = axisConf.getInFlowPhases();
addHandlerToSecurity(phasesIn);

// similar to above but now these are phases processed 
// when there is fault in incoming communication
List<Phase> phasesInFault = axisConf.getInFaultFlowPhases();
phasesInFault.add(0, phase);
addHandlerToSecurity(phasesInFault);

添加到安全阶段:

public void addHandlerToSecurity(List<Phase> phases) {
        try {
            for (Phase p : phases) {
                if ("Security".equals(p.getName())) {
                    p.addHandler(new MessageContentLoggerHandler());
                }
            }
        } catch (Exception e) {
            // TODO
        }
    }

您的自定义处理程序必须实现 invoke(MessageContext msgContext) 方法。MessageContent 包含整个 SOAP 信封,即来自 Web 服务的请求和响应。

public InvocationResponse invoke(MessageContext msgContext) throws AxisFault {
        String envelope = msgContext.getEnvelope().toString();
        // do something with content of SOAP envelope
        return InvocationResponse.CONTINUE;
}

推荐阅读