我有一个Web服务,需要某种请求格式。 当我发送无效的请求格式时,我有以下错误:
<faultcode>soap:Client</faultcode>
<faultstring>Unmarshalling Error: unexpected element (uri:"", local:"aaaa"). Expected elements are <{}xxx>,<{}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拦截器没有太多记录。 :(
提前致谢!
Hejk
答案 0 :(得分:1)
您可以使用默认的CXF记录器LoggingInInterceptor
和LoggingOutInterceptor
记录soap消息,或使用自定义拦截器提取有效负载
// 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());