webservice实现应用程序之间的通信,使用xml传输数据。服务端通过http发布接口,在客户端通过http调用接口。
webservice涉及的技术
WSDL(Web Service Description Language)
基于xml的语言,用于描述webservices
SOAP(Simple Object Access Protocol)
基于xml的协议,用于访问网络服务
UDDI(Universal Description, Discovery and Integration)
目录服务
java实现webservice
使用JAXWS(Java API for XML Web Service)
@WebService
public interface WSServer {
String test(@WebParam(name="text") String text);
}
@WebService(endpointInterface="jws.WSServer", serviceName="WSServer")
public class WSServerImpl implements WSServer {
public String test(String text) {
return text + ", I am server";
}
}
public class StartServer {
public static void main(String[] args) {
System.out.println("starting server");
WSServer server = new WSServerImpl();
String address = "http://localhost:8080/test";
Endpoint.publish(address, server);
System.out.println("server started");
}
}
public class WSClient {
public static void main(String[] args) {
try {
URL url = new URL("http://localhost:8080/test?wsdl");
QName qName = new QName("http://jws/", "WSServer");
Service service = Service.create(url, qName);
WSServer server = service.getPort(WSServer.class);
String str = server.test("Hello");
System.out.println(str);
} catch(Exception e) {
e.printStackTrace();
}
}
}
网友评论