非常感谢孙浩老师。
修改服务接口方法
@WebResult(name="user")
public List<User> getAllUser(@WebParam(header=true,name="authInfo")String authInfo);
修改实现方法
@Override
public List<User> getAllUser(String authInfo) {
System.out.println(authInfo);
return users;
}
测试类
/**
* @Title: test06
* @Description: SOAP显示的创建和处理SOAPHeader的信息
* @param
* @return void
* @throws
*/
@Test
public void test06() {
try {
// 创建访问wsdl服务的URL
URL url = new URL("http://localhost:9999/ns?wsdl");
// 通过Qname指明服务的具体信息
QName name = new QName("http://soap.lichen.cn/",
"MyServiceImplService");
// 创建service
Service service = Service.create(url, name);
// 创建dispatch
Dispatch<SOAPMessage> dispatch = service.createDispatch(new QName(
"http://soap.lichen.cn/", "MyServiceImplPort"),
SOAPMessage.class, Service.Mode.MESSAGE);
// 创建SOAPmessage
SOAPMessage message = MessageFactory.newInstance().createMessage();
SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
SOAPBody body = envelope.getBody();
QName qname = new QName("http://soap.lichen.cn/", "getAllUser", "xsd");
@SuppressWarnings("unused")
SOAPBodyElement bodyElement = body.addBodyElement(qname);
// 输入创建SOAPmessage
message.writeTo(System.out);
System.out.println("\n\n" + "-----------invoking-------------"
+ "\n");
// 传递消息并且得到结果
SOAPMessage responseMessage = dispatch.invoke(message);
// 输出得到的SOAPmessage
responseMessage.writeTo(System.out);
// 将响应的消息转换为dom对象
Document doc = responseMessage.getSOAPBody()
.extractContentAsDocument();
NodeList nl = doc.getElementsByTagName("user");
JAXBContext ctx = JAXBContext.newInstance(User.class);
for (int i = 0; i < nl.getLength(); i++) {
Node n = nl.item(i);
User u = (User) ctx.createUnmarshaller().unmarshal(n);
System.out.println("\n"+u.getNickname());
}
} catch (SOAPException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JAXBException e) {
e.printStackTrace();
}
}
测试端:
服务端: