1.WebService是什么?
WebService是一种跨编程语言和跨操作系统平台的远程调用技术。
2.其中要知道的几个概念:
WSDL、SOAP、CXP、SEI;
3.如果WSDL可以访问,则代表服务器端可用。
4.开发的大体思路:
1.Server:@WebService
@WebMethod
发布WebService:用Endpoint.publish(… , …);
2.Client:路径?wsdl
WebService浏览器
5.具体实现:
1)SEI(Interface)--------------->SEI的实现(.class)[—要加@WebService, @WebMethod-----]
代码实现接口:
package com.atguigu.day01_ws.ws;
public interface HelloWS {
}
代码实现继承类:
package com.atguigu.day01_ws.ws;
import javax.jws.WebService;
/*
-
SEI的实现
*/
@WebService
public class HelloWSImpl implements HelloWS {public String sayHello(String name) {
System.out.println(“server sayHello()”+name);
return "Hello "+name;
}
}
2) ServerTest.java-------------->发布:Endpoint.publish(address,implement);
注意:address可以随便取,而implement是对类进行实例化的对象
代码实现:package com.atguigu.day01_ws.ws.server;
import javax.xml.ws.Endpoint;
import com.atguigu.day01_ws.ws.HelloWSImpl;
/*
-
发布Web Service
*/
public class ServerTest {
public static void main(String[]args) {
String address=“http://localhost:8989/day01_ws/hellows”;
Endpoint.publish(address, new HelloWSImpl());
System.out.println(“发布webservice成功!”);
}
}3)ClientTest.java------------->客户端测试发布:建立一个实现服务的工厂
HelloWSImplService factory=new HelloWSImplService();
HelloWSImpl helloWS=factory.getHelloWSImplPort();
代码实现:
package com.atguigu.day01_ws_client.test;
import com.atguigu.day01_ws.ws.HelloWSImpl;
import com.atguigu.day01_ws.ws.HelloWSImplService;
/* -
调用web service
*/
public class ClientTest {public static void main(String[]args) {
HelloWSImplService factory=new HelloWSImplService();
HelloWSImpl helloWS=factory.getHelloWSImplPort();
System.out.println(helloWS.getClass());String result=helloWS.sayHello("Jack"); System.out.println("client "+result);
}
}
6.生成wsdl:
1)直接生成:cmd------> cd 到生成目录下+wsimport -keep …?wsdl
2)在本地生成:打开TCP/IP Monitor(进行监听)------->建立一个wsdl文件-------->
cd 到生成目录下+wsimport -keep …?wsdl(这个wsdl文件的路径)
7.通过CXF来实现WebService
在project里新建一个文件夹lib,加入cxf的.jar包。在cmd:cd 到生成目录下+wsdl2java+…?wsdl