我无法从Soap Web服务打印响应。
通过编辑生成的存根代码,看到了一些解决方案。但是我无法编辑生成的代码,因为它在每次构建时都恢复为原始形式。寻找一种解决方案,我可以在不更改生成代码的情况下打印解决方案。
我正在使用Spring Boot微服务中的SOAP服务。
ServiceContext serviceConxt = omsSchedulingService._getServiceClient().getServiceContext();
OperationContext operationContext = serviceConxt.getLastOperationContext();
MessageContext inMessageContext = operationContext.getMessageContext("Out");
log.info(inMessageContext.getEnvelope().toString());
答案 0 :(得分:1)
您可以为肥皂消息添加消息处理程序。
然后,使用处理程序拦截消息后,就可以打印出响应。
您需要将处理程序添加到处理程序链中,具体取决于您的项目,您可以通过编程方式或使用config进行处理。
final class MyMessageHandler implements SOAPHandler<SOAPMessageContext>{
@Override
public void close(MessageContext context) {
handle(context);
}
private boolean handle(MessageContext context) {
if (context != null) {
try {
Object httpResponseCodeObj = context.get(SOAPMessageContext.HTTP_RESPONSE_CODE);
if (httpResponseCodeObj instanceof Integer)
httpResponseCode = ((Integer) httpResponseCodeObj).intValue();
if (context instanceof SOAPMessageContext) {
SOAPMessage message = ((SOAPMessageContext) context).getMessage();
ByteArrayOutputStream byteOut = new ByteArrayOutputStream(512);
message.writeTo(byteOut);
String messageStr = byteOut.toString(getCharacterEncoding(message));
boolean outbound = Boolean.TRUE.equals(context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY));
Logger.info(loggingPrefix, outbound ? "SOAP request: " : "SOAP response: ", replaceNewLines(messageStr));
}
} catch (SOAPException e) {
Logger.error(e, loggingPrefix, "SOAPException: ", e.getMessage(), NEWLINE);
} catch (IOException e) {
Logger.error(e, loggingPrefix, "IOException: ", e.getMessage(), NEWLINE);
}
}
return true;
}
}
答案 1 :(得分:0)
如果您不想实现拦截器,最简单的方法是通过vm参数使用日志记录:
JAVA_OPTS=-Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog -Dorg.apache.commons.logging.simplelog.showdatetime=true -Dorg.apache.commons.logging.simplelog.log.httpclient.wire=debug -Dorg.apache.commons.logging.simplelog.log.org.apache.commons.httpclient=debug
这样,您应该在控制台中看到带有标头的请求/响应日志。
答案 2 :(得分:0)
首先,您可以从客户端存根获取AxisConfiguration。
AxisConfiguration axisConf = stub._getServiceClient().getAxisConfiguration();
处理传入和传出消息分为多个阶段。有一个阶段列表(流程),当一切都正常运行(没有错误)时将处理该阶段,对于发生某些故障的情况(例如:在消息处理过程中引发异常时。每个流都可能流入或流出,所以总共有4个流。
List<Phase> phasesIn = axisConf.getInFlowPhases(); // normal incoming communication i.e. response from webservice
List<Phase> phasesOut = axisConf.getOutFlowPhases(); // normal outgoing communication
List<Phase> phasesFaultIn = axisConf.getInFaultFlowPhases(); // faulty incoming communication e.g. when an exception occurs during message processing
List<Phase> phasesFaultOut = axisConf.getOutFaultFlowPhases(); // faulty outgoing communication
在org.apache.axis2.phaseresolver.PhaseMetadata中定义了一些但不是全部的阶段名称。 例如,在PhaseMetadata中找不到在Rampart模块(用于Web Service安全的模块)中处理的“安全”阶段。 您可以在每个阶段添加处理程序,例如
for (Phase p : phasesOut) {
if (PhaseMetadata.PHASE_TRANSPORT_OUT.equals(p.getName())) {
p.addHandler(new MessageContentLoggerHandler());
}
}
Handler是扩展org.apache.axis2.handlers.AbstractHandler的类。 您只需要实现
public InvocationResponse invoke(MessageContext msgContext).
您可以访问MessageContext。当然,您可以像这样获得整个SOAP信封:
msgContext.getEnvelope().toString()
,例如将其打印到日志或另存为单独的文件。 记住要放
return InvocationResponse.CONTINUE;
在处理程序成功处理消息的情况下,调用方法末尾的。否则,处理将在此处理程序中停止,并且整个过程不会进入任何其他阶段。 如果需要查看带有WSS标题的整个消息,则可以添加自己的阶段。例如,这会将您的自定义阶段添加为处理传出消息的最后一个阶段(也在Rampart的安全阶段之后)
Phase phase = new Phase("SomePhase");
phase.addHandler(new SomeCustomHandler());
axisConf.getOutFlowPhases().add(phase);
当然,在生产环境中记录(并以任何其他方式公开)安全标头是一个非常糟糕的主意。只能在某些测试环境中将其用于调试目的。