使用java提供的handler机制在客户端对SOAP请求包注入header,在服务端对SOAP请求包进行解析,获得头部,并展示出头部的信息
时间: 2023-12-19 10:04:25 浏览: 95
java代码-使用java解决http请求SOAP webService接口的源代码
在Java中,可以使用JAX-WS API来创建SOAP客户端和服务端。下面是一个示例代码,演示如何在客户端使用handler机制注入SOAP头部,以及如何在服务端使用handler机制解析SOAP头部。
首先,我们需要定义一个SOAPHandler类,用于处理SOAP消息头部。在客户端,我们可以在SOAP消息发送前,将需要注入的头部信息写入SOAP消息的Header中;在服务端,我们可以在SOAP消息接收后,从SOAP消息的Header中读取我们需要的头部信息。
以下是一个SOAPHandler的示例代码:
```java
import java.util.Set;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;
public class MyHandler implements SOAPHandler<SOAPMessageContext> {
private static final String MY_HEADER_NS = "http://example.com/myheader";
private static final String MY_HEADER_NAME = "MyHeader";
@Override
public boolean handleMessage(SOAPMessageContext context) {
Boolean isOutbound = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (isOutbound) {
// 客户端处理逻辑,将需要注入的头部信息写入SOAP消息的Header中
try {
SOAPEnvelope envelope = context.getMessage().getSOAPPart().getEnvelope();
SOAPFactory factory = SOAPFactory.newInstance();
SOAPHeader header = envelope.getHeader();
if (header == null) {
header = envelope.addHeader();
}
SOAPHeaderElement myHeaderElement = header.addHeaderElement(new QName(MY_HEADER_NS, MY_HEADER_NAME, "my"));
SOAPElement child = myHeaderElement.addChildElement("MyValue");
child.addTextNode("Hello World!");
context.getMessage().saveChanges();
} catch (Exception e) {
e.printStackTrace();
}
} else {
// 服务端处理逻辑,从SOAP消息的Header中读取我们需要的头部信息
try {
SOAPEnvelope envelope = context.getMessage().getSOAPPart().getEnvelope();
SOAPHeader header = envelope.getHeader();
if (header != null) {
SOAPHeaderElement myHeaderElement = (SOAPHeaderElement) header.getChildElements(new QName(MY_HEADER_NS, MY_HEADER_NAME, "my")).next();
String myValue = myHeaderElement.getElementsByTagName("MyValue").item(0).getTextContent();
System.out.println("Received MyValue from client: " + myValue);
}
} catch (Exception e) {
e.printStackTrace();
}
}
return true;
}
@Override
public Set<QName> getHeaders() {
return null;
}
@Override
public boolean handleFault(SOAPMessageContext context) {
return true;
}
@Override
public void close(MessageContext context) {
}
}
```
在客户端,我们需要将SOAPHandler注册到SOAP请求的处理链中。以下是一个客户端的示例代码:
```java
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Service;
public class MyClient {
private static final String SERVICE_URL = "http://localhost:8080/MyService?wsdl";
public static void main(String[] args) throws Exception {
URL url = new URL(SERVICE_URL);
QName qname = new QName("http://example.com/myservice", "MyService");
Service service = Service.create(url, qname);
MyService port = service.getPort(MyService.class);
// 注册SOAPHandler
BindingProvider bp = (BindingProvider) port;
bp.getBinding().getHandlerChain().add(new MyHandler());
// 调用服务
port.sayHello();
}
}
```
在服务端,我们也需要将SOAPHandler注册到SOAP请求的处理链中。以下是一个服务端的示例代码:
```java
import javax.xml.ws.Endpoint;
import javax.xml.ws.handler.Handler;
import javax.xml.ws.handler.HandlerResolver;
import javax.xml.ws.handler.PortInfo;
public class MyServer {
public static void main(String[] args) throws Exception {
Endpoint endpoint = Endpoint.publish("http://localhost:8080/MyService", new MyServiceImpl());
// 注册SOAPHandler
endpoint.getBinding().setHandlerChain(new HandlerResolver() {
@Override
public Handler<?>[] getHandlerChain(PortInfo portInfo) {
return new Handler<?>[]{new MyHandler()};
}
});
System.out.println("MyService started.");
}
}
```
注意,在使用handler机制时,必须在SOAP请求处理链中注册handler,以便让SOAP消息在处理过程中经过handler的处理。另外,由于SOAP消息头部的格式是自定义的,因此在实际应用中,需要根据具体的业务需求,设计合适的SOAP头部格式。
阅读全文