一、webservice的定义
1. 接口定义:
package com.npf.server;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import com.npf.model.User;
@WebService
public interface MyService {
@WebResult(name="addReturnResult")
public int add(@WebParam(name="a")int a, @WebParam(name="b")int b);
@WebResult(name="loginReturnUser")
public User login(@WebParam(name="username")String username,@WebParam(name="password")String password);
}
2. 接口实现:
package com.npf.server;
import javax.jws.WebService;
import com.npf.model.User;
@WebService(
endpointInterface="com.npf.server.MyService",
targetNamespace="http://server.npf.com/",
portName="MyServiceImplPort")
public class MyServiceImpl implements MyService {
@Override
public int add(int a, int b) {
System.out.println("a+b"+"="+(a+b));
return a+b;
}
@Override
public User login(String username, String password) {
User user = new User();
user.setId("1");
user.setUsername("Jack");
user.setPassword("npf123");
return user;
}
}
3. 发布服务:
package com.npf.server;
import javax.xml.ws.Endpoint;
public class ServerStart {
public static void main(String[] args) {
MyService service = new MyServiceImpl();
Endpoint.publish("http://localhost:8888/ns", service);
}
}
二、soap消息传递和处理(基于Message的方式)
package com.npf.client;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import org.w3c.dom.Document;
public class Client2 {
public static void main(String[] args) throws Exception {
//1.创建消息工厂
MessageFactory messageFactory = MessageFactory.newInstance();
//2.创建soapMessage
SOAPMessage soapMessage = messageFactory.createMessage();
//3.创建soapPart
SOAPPart soapPart = soapMessage.getSOAPPart();
//4.获取soapEnvelope
SOAPEnvelope envelope = soapPart.getEnvelope();
//5.可以通过soapEnvelope获取有效的header和body信息
SOAPBody body = envelope.getBody();
//6.创建一个Qname(Qname就是一个有效的节点)
QName qname = new QName("http://server.npf.com/","add","tns");
//7.将节点插入body,作为一个空架的节点
SOAPBodyElement element = body.addBodyElement(qname);
//8.在刚才的空架的节点中插入元素
element.addChildElement("a").setValue("22");
element.addChildElement("b").setValue("33");
//soapMessage.writeTo(System.out);
//9.创建服务
URL uri = new URL("http://localhost:8888/ns?wsdl");
QName sname = new QName("http://server.npf.com/","MyServiceImplService");
Service service = Service.create(uri, sname);
//10.创建Dispatch
Dispatch<SOAPMessage> dispatch = service.createDispatch(new QName(
"http://server.npf.com/","MyServiceImplPort"), SOAPMessage.class, Service.Mode.MESSAGE);
SOAPMessage responseSoapMessage = dispatch.invoke(soapMessage);
responseSoapMessage.writeTo(System.out);
System.out.println("");
//11.将响应的消息转换成dom对象
Document document = responseSoapMessage.getSOAPPart().getEnvelope().getBody().extractContentAsDocument();
String content = document.getElementsByTagName("addReturnResult").item(0).getTextContent();
System.out.println(content);
}
}