我有一个 EJB 无状态 bean Web 服务,并且想要访问整个 SOAP 消息,而不仅仅是我目前在参数中获取的 SOAP 正文。我想将消息提交到不同的组件。我使用的是 Spring 配置,但不是 Spring-WS。我遵循了本教程:
http://java.dzone.com/articles/creating-soap-message-handlers
照原样,暂时没有做任何改变。它只记录 SOAP 消息。handleMessage() 没有被调用。
@Stateless
@WebService(portName = "XRequest_PortType", serviceName = "XRequestService", endpointInterface = "XRequestPortType")
@Addressing(enabled = true)
@HandlerChain(file = "LogMessage_handler.xml")
@Interceptors(SpringBeanAutowiringInterceptor.class)
public class XRequest_PortTypeWS implements
XRequestPortType {
...}
LogMessage_handler.xml 与教程略有不同,但我尝试了两种方式:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<javaee:handler-chains
xmlns:javaee="http://java.sun.com/xml/ns/javaee"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<javaee:handler-chain>
<javaee:handler>
<javaee:handler-class>com.test.ws.LogMessageHandle</javaee:handler-class>
</javaee:handler>
</javaee:handler-chain>
</javaee:handler-chains>
日志消息处理程序:
package com.test.ws;
import java.io.IOException;
import java.util.Collections;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;
/**
*
* @author meerasubbarao
*/
public class LogMessageHandler implements SOAPHandler<SOAPMessageContext> {
public boolean handleMessage(SOAPMessageContext messageContext) {
log(messageContext);
return true;
}
public Set<QName> getHeaders() {
return Collections.EMPTY_SET;
}
public boolean handleFault(SOAPMessageContext messageContext) {
return true;
}
public void close(MessageContext context) {
}
private void log(SOAPMessageContext messageContext) {
SOAPMessage msg = messageContext.getMessage(); // Line 1
try {
msg.writeTo(System.out); // Line 3
} catch (SOAPException ex) {
Logger.getLogger(LogMessageHandler.class.getName()).log(
Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(LogMessageHandler.class.getName()).log(
Level.SEVERE, null, ex);
}
}
}
这甚至是获取整个 SOAP 消息的最佳方式吗?我仍然需要从 XRequest_PortTypeWS 调用 Spring bean 来发送 SOAP 消息。