我正在尝试找到一种向SOAP消息添加标头元素的方法。
如果我使用@WebParam
并将header
设置为true
,那么它工作正常,该参数很好地位于<soap:header>
中。但是,需要在我的API中添加一堆参数,这几乎不是一种选择。
在第一个视图拦截器似乎不可能,所以我在看SOAPHandlers,但很难找到关于此的明确信息。
这是我的春季配置:
<bean id="testWebservice" class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean" >
<property name="serviceInterface" value="com...TestWebService"/>
<property name="wsdlDocumentUrl" value="http://localhost:9062/test-ws/remoting/test?wsdl"/>
<property name="serviceName" value="TestWebService"/>
<property name="namespaceUri" value="http://test.com"/>
<property name="portName" value="TestServicePort"/>
<property name="lookupServiceOnStartup" value="false"/>
<property name="handlerResolver" ref="myResolver"/>
</bean>
<bean id="myResolver" class="com...TestHandlerResolver" />
我的处理程序(链接在TestHandlerResolver中):
public class TestHandler implements SOAPHandler<SOAPMessageContext> {
@Override
public void close(MessageContext context) {
}
@Override
public Set<QName> getHeaders() {
return null;
}
@Override
public boolean handleFault(SOAPMessageContext context) {
return true;
}
@Override
public boolean handleMessage(SOAPMessageContext context) {
if (Boolean.TRUE.equals(context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY))) {
try {
List<Header> headers = new ArrayList<Header>();
Header dummyHeader;
dummyHeader = new Header(new QName("dummy"), "decapitated", new JAXBDataBinding(String.class));
headers.add(dummyHeader);
context.put(Header.HEADER_LIST, headers);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return true;
}
}
问题在于我有点不知所措。使用处理程序是正确的方法来执行此操作,如果是这样,您能给出或指出一个很好的例子吗?有没有办法用拦截器做到这一点?或者我完全是这里的目标......
答案 0 :(得分:2)
让它像这样工作:
@Override
public boolean handleMessage(SOAPMessageContext context) {
try {
SOAPMessage msg = context.getMessage();
SOAPPart part = msg.getSOAPPart();
SOAPEnvelope envelope = part.getEnvelope();
SOAPHeader header = envelope.getHeader();
if (Boolean.TRUE.equals(context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY))) {
if (header == null) {
header = envelope.addHeader();
}
this.addToHeader(envelope, header, "MessageID", UUID.randomUUID().toString());
...
} else {
if (header != null) {
Iterator<?> i = header.getChildElements();
while (i.hasNext()) {
SOAPElement el = (SOAPElement) i.next();
String tagName = el.getLocalName();
String value = el.getValue();
...
}
}
}
} catch (Exception e) {
...
}
return true;
}
private void addToHeader(SOAPEnvelope envelope, SOAPHeader header, String key, String value) throws SOAPException {
Name qname = envelope.createName(key, "ns1", "http://ecom.bnpp.com");
SOAPHeaderElement element = header.addHeaderElement(qname);
element.addTextNode(value);
}