通过CFX发布WebService(一)
發(fā)布WebService的方法很多。如XFire,CFX等。現(xiàn)在首先介紹下怎樣通過CFX來發(fā)部一個(gè)WebService。
(1) 首先,是從Apache官方網(wǎng)站獲取CFX的Java包。其地址是:http://cxf.apache.org/download.html。在該網(wǎng)頁上可以下載最新的CFX 的Java開發(fā)包。
(2) 其次,新建一個(gè)簡單的java工程,不必是web工程,只是通過eclipse或者myeclipse新建一個(gè)java project。將CFX的java開發(fā)包復(fù)制到該工程的classpath目錄下。
當(dāng)新建的是一個(gè)web工程時(shí),只需要將該工程復(fù)制到lib文件夾中。
(3) 在該工程中新建一個(gè)普通的接口。
package com.example.webservice;
import javax.jws.WebParam; import javax.jws.WebService; @WebService public interface HelloWorldService { public String sayHello(@WebParam(name="userName")String userName); }
注意到:上面的接口中有一個(gè)注解:@WebService,該注解表示這個(gè)是一個(gè)WebService服務(wù)
(4) 同時(shí)新建一個(gè)類,HelloWorldServiceImpl實(shí)現(xiàn)該接口。
package com.example.webservice;
import javax.jws.WebMethod; import javax.jws.WebService; @WebService(endpointInterface="com.example.webservice.HelloWorldService", serviceName="helloWorldService") public class HelloWorldServiceImpl implements HelloWorldService { @WebMethod public String sayHello(String userName) { System.out.println("HelloWorldServiceImp.sayHello("+userName+")"); return "Hello,"+userName; } }
注意:這個(gè)實(shí)現(xiàn)類中,也有注解,一個(gè)是@WebService注解,該注解中有2個(gè)部分,一個(gè)是endpointInterface="com.example.webservice.HelloWorldService",
這里的內(nèi)容是上面的接口的完整包名和接口名組成的。另一個(gè)是serviceName="helloWorldService",這個(gè)是用來現(xiàn)實(shí)該webService的入口的,即該webservice的名稱為
helloWorldService。當(dāng)調(diào)用該webservice時(shí),其地址應(yīng)該是:htt://IpAddress:port/helloWorldService
還有另一個(gè)注解:@WebMethod,該注解用來標(biāo)識了webService的實(shí)現(xiàn)方法。
現(xiàn)在主要的webservice的功能已經(jīng)實(shí)現(xiàn)了,所要做的是將webservice進(jìn)行發(fā)布。發(fā)布該webservice的方法有好幾種,可以用Spring發(fā)布,也可以直接運(yùn)行一個(gè)main還是發(fā)布。
現(xiàn)在演示的是用一個(gè)main函數(shù)發(fā)布該webservice。
新建一個(gè)類,該類中有一個(gè)main函數(shù)。
import javax.xml.ws.Endpoint;
import com.lenovo.webservice.HelloWorldServiceImpl;
public class ServerTest {
public static void main(String[] args) {
System.out.println("Starting Server");
HelloWorldServiceImpl helloWorldServiceImpl=new HelloWorldServiceImpl();
String address="http://localhost:8090/helloWorldService";
Endpoint.publish(address, helloWorldServiceImpl);
System.out.println("Start success");
}
}
運(yùn)行該類之后,該webservice就已經(jīng)發(fā)布了。
此時(shí),可以在其他的程序中調(diào)用該webservice了。
高度決定視野,角度改變觀念,尺度把握人生。
總結(jié)
以上是生活随笔為你收集整理的通过CFX发布WebService(一)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Cyclone II FPGA系列简介(
- 下一篇: Matlab 卷积函数 ——conv2