使用Java提供的handler机制在客户端对SOAP请求包注入header。在实验一建立的web服务的基础上,对客户端产生的SOAP请求包加入header,header有一个子元素student,命名空间为你自己项目使用的命名空间;student有三个子元素sno和sname、time,即学号,姓名和当前系统时间。参考如下形式: <SOAP-ENV:Header> <student xmlns="http://hello.soapHandlerEx/"> <sno>001</sno> <sname>你的名字</sname> <time>2023/5/10</time> </student> </SOAP-ENV:Header> 输出方式可以采用命令行或文本记录。 2、在服务端对SOAP请求包进行解析,获得头部,并展示出头部的信息,展示方式可以自行设计。
时间: 2024-04-01 11:37:55 浏览: 105
使用jaxws建立webservice客户端并实现soap消息的handler验证示例
好的,我理解了你的问题。针对第一个问题,以下是Java代码实现:
```java
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.soap.SOAPBinding;
public class SOAPHandlerClient {
private static final String NAMESPACE_URI = "http://hello.soapHandlerEx/";
public static void main(String[] args) throws Exception {
// 创建SOAP消息
SOAPMessage request = createSOAPRequest();
// 创建服务端点
QName serviceName = new QName("http://soapHandlerEx.hello/", "HelloServiceImplService");
QName portName = new QName("http://soapHandlerEx.hello/", "HelloServiceImplPort");
Service service = Service.create(serviceName);
service.addPort(portName, SOAPBinding.SOAP11HTTP_BINDING, "http://localhost:8080/hello");
// 创建分派器
Dispatch<SOAPMessage> dispatcher = service.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE);
// 在请求消息的头部添加student元素
SOAPHeader header = request.getSOAPHeader();
if (header == null) {
header = request.getSOAPPart().getEnvelope().addHeader();
}
SOAPElement studentElement = header.addChildElement(new QName(NAMESPACE_URI, "student"));
studentElement.addNamespaceDeclaration("ns", NAMESPACE_URI);
SOAPElement snoElement = studentElement.addChildElement("sno");
snoElement.setTextContent("001");
SOAPElement snameElement = studentElement.addChildElement("sname");
snameElement.setTextContent("你的名字");
SOAPElement timeElement = studentElement.addChildElement("time");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/M/d");
timeElement.setTextContent(LocalDateTime.now().format(formatter));
// 设置请求上下文,添加头部信息
BindingProvider bindingProvider = (BindingProvider) dispatcher;
bindingProvider.getRequestContext().put(MessageContext.HTTP_REQUEST_HEADERS, request.getMimeHeaders());
// 发送SOAP请求
SOAPMessage response = dispatcher.invoke(request);
// 输出响应消息
System.out.println("响应消息:");
response.writeTo(System.out);
}
private static SOAPMessage createSOAPRequest() throws Exception {
// 创建SOAP消息工厂
javax.xml.soap.SOAPConnectionFactory factory = javax.xml.soap.SOAPConnectionFactory.newInstance();
javax.xml.soap.SOAPConnection connection = factory.createConnection();
javax.xml.soap.MessageFactory messageFactory = javax.xml.soap.MessageFactory.newInstance();
SOAPMessage message = messageFactory.createMessage();
SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
envelope.addNamespaceDeclaration("ns", "http://soapHandlerEx.hello/");
SOAPBody body = envelope.getBody();
QName operationName = new QName("http://soapHandlerEx.hello/", "sayHello");
body.addBodyElement(operationName);
message.saveChanges();
return message;
}
}
```
针对第二个问题,我们可以在服务端的`HelloServiceImpl`类的`sayHello`方法中解析SOAP头部信息并展示出来。以下是Java代码实现:
```java
import java.time.format.DateTimeFormatter;
import javax.jws.WebService;
import javax.xml.namespace.QName;
import javax.xml.soap.Node;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.BindingType;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.soap.SOAPBinding;
@WebService(endpointInterface = "hello.HelloService")
@BindingType(SOAPBinding.SOAP11HTTP_BINDING)
public class HelloServiceImpl implements HelloService {
private static final String NAMESPACE_URI = "http://hello.soapHandlerEx/";
@Override
public String sayHello() {
// 从请求上下文中获取SOAP消息
MessageContext mc = MessageContext.getCurrentContext();
SOAPMessage message = mc.getRequest();
// 获取SOAP头部信息
SOAPHeader header = message.getSOAPHeader();
if (header != null) {
try {
SOAPElement studentElement = (SOAPElement) header.getChildElements(new QName(NAMESPACE_URI, "student")).next();
String sno = studentElement.getElementsByTagNameNS(NAMESPACE_URI, "sno").item(0).getTextContent();
String sname = studentElement.getElementsByTagNameNS(NAMESPACE_URI, "sname").item(0).getTextContent();
String time = studentElement.getElementsByTagNameNS(NAMESPACE_URI, "time").item(0).getTextContent();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/M/d");
System.out.println("请求消息头部信息:sno=" + sno + ", sname=" + sname + ", time=" + formatter.parse(time));
} catch (Exception e) {
e.printStackTrace();
}
}
// 返回响应消息
String response = "Hello!";
SOAPMessage soapResponse = null;
try {
soapResponse = createSOAPResponse(response);
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
private static SOAPMessage createSOAPResponse(String response) throws Exception {
SOAPMessage message = MessageFactory.newInstance().createMessage();
SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
SOAPBody body = envelope.getBody();
QName operationName = new QName("http://soapHandlerEx.hello/", "sayHelloResponse");
body.addBodyElement(operationName).addTextNode(response);
message.saveChanges();
return message;
}
}
```
在客户端运行`SOAPHandlerClient`类,可以看到输出结果中包含了请求消息头部信息。
阅读全文