理論聯(lián)系實際,單單只講理論那就成了紙上談兵,用一個HelloWorld Demo可來說明事更加直觀。那下面咱們就開始進行講解:
首先到apache官方網(wǎng)下載apache-cxf-2.2.2,地址:http://cxf.apache.org/
新建一個Java Project,導(dǎo)入cxf常用.jar包
Java代碼
1.commons-logging-1.1.1.jar
2.geronimo-activation_1.1_spec-1.0.2.jar (or Sun's Activation jar)
3.geronimo-annotation_1.0_spec-1.1.1.jar (JSR 250)
4.geronimo-javamail_1.4_spec-1.6.jar (or Sun's JavaMail jar)
5.geronimo-servlet_2.5_spec-1.2.jar (or Sun's Servlet jar)
6.geronimo-ws-metadata_2.0_spec-1.1.2.jar (JSR 181)
7.geronimo-jaxws_2.1_spec-1.0.jar (or Sun's jaxws-api-2.1.jar)
8.geronimo-stax-api_1.0_spec-1.0.1.jar (or other stax-api jar)
9.jaxb-api-2.1.jar
10.jaxb-impl-2.1.12.jar
11.jetty-6.1.21.jar
12.jetty-util-6.1.21.jar
13.neethi-2.0.4.jar
14.saaj-api-1.3.jar
15.saaj-impl-1.3.2.jar
16.wsdl4j-1.6.2.jar
17.wstx-asl-3.2.8.jar
18.XmlSchema-1.4.5.jar
19.xml-resolver-1.2.jar
20.cxf-2.2.2.jar
commons-logging-1.1.1.jar
geronimo-activation_1.1_spec-1.0.2.jar (or Sun's Activation jar)
geronimo-annotation_1.0_spec-1.1.1.jar (JSR 250)
geronimo-javamail_1.4_spec-1.6.jar (or Sun's JavaMail jar)
geronimo-servlet_2.5_spec-1.2.jar (or Sun's Servlet jar)
geronimo-ws-metadata_2.0_spec-1.1.2.jar (JSR 181)
geronimo-jaxws_2.1_spec-1.0.jar (or Sun's jaxws-api-2.1.jar)
geronimo-stax-api_1.0_spec-1.0.1.jar (or other stax-api jar)
jaxb-api-2.1.jar
jaxb-impl-2.1.12.jar
jetty-6.1.21.jar
jetty-util-6.1.21.jar
neethi-2.0.4.jar
saaj-api-1.3.jar
saaj-impl-1.3.2.jar
wsdl4j-1.6.2.jar
wstx-asl-3.2.8.jar
XmlSchema-1.4.5.jar
xml-resolver-1.2.jar
cxf-2.2.2.jar
接下就是HelloWorld Demo開發(fā)了
第一步:新建一個webservice接口
Java代碼
1.@WebService
2.public interface IHelloWorld {
3. //@WebParam給參數(shù)命名,提高可代碼可讀性。此項可選
4.blic String sayHi(@WebParam(name="text") String text);
5.}
@WebService
public interface IHelloWorld {
//@WebParam給參數(shù)命名,提高可代碼可讀性。此項可選
public String sayHi(@WebParam(name="text") String text);
}
通過注解@WebService申明為webservice接口
第二步,實現(xiàn)WebService接口
Java代碼
1. @WebService
2. public class HelloWorldImpl implements IHelloWorld {
3.
4.public String sayHi(String name) {
5. System.out.println("sayHello is called by " + name);
6. return "Hello " + name;
7.}
8.
9. }
@WebService
public class HelloWorldImpl implements IHelloWorld {
public String sayHi(String name) {
System.out.println("sayHello is called by " + name);
return "Hello " + name;
}
}
第三步,創(chuàng)建服務(wù)端
Java代碼
1. public class Server {
2.
3.private Server(){
4. IHelloWorld helloWorld = new HelloWorldImpl();
5. //創(chuàng)建WebService服務(wù)工廠
6. JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
7. //注冊WebService接口
8. factory.setServiceClass(IHelloWorld.class);
9. //發(fā)布接口
10. factory.setAddress("http://localhost:9000/HelloWorld");
11. factory.setServiceBean(helloWorld);
12. //創(chuàng)建WebService
13. factory.create();
14.};
15.
16.public static void main(String[] args) throws InterruptedException{
17. //啟動服務(wù)端
18. new Server();
19. System.out.println("Server ready...");
20. //休眠一分鐘,便于測試
21. Thread.sleep(1000*60);
22. System.out.println("Server exit...");
23. System.exit(0);
24.}
25. }
public class Server {
private Server(){
IHelloWorld helloWorld = new HelloWorldImpl();
//創(chuàng)建WebService服務(wù)工廠
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
//注冊WebService接口
factory.setServiceClass(IHelloWorld.class);
//發(fā)布接口
factory.setAddress("http://localhost:9000/HelloWorld");
factory.setServiceBean(helloWorld);
//創(chuàng)建WebService
factory.create();
};
public static void main(String[] args) throws InterruptedException{
//啟動服務(wù)端
new Server();
System.out.println("Server ready...");
//休眠一分鐘,便于測試
Thread.sleep(1000*60);
System.out.println("Server exit...");
System.exit(0);
}
}
第四步,創(chuàng)建客戶端
Java代碼
1. public class Client {
2.
3.private Client(){};
4.
5.public static void main(String[] args){
6. //創(chuàng)建WebService客戶端代理工廠
7. JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
8. //注冊WebService接口
9. factory.setServiceClass(HelloWorld.class);
10. //設(shè)置WebService地址
11. factory.setAddress("http://localhost:9000/HelloWorld");
12. IHelloWorld iHelloWorld = (IHelloWorld)factory.create();
13. System.out.println("invoke webservice...");
14. System.out.println("message context is:"+iHelloWorld.sayHi("
15. Josen"));
16. System.exit(0);
17.}
18. }
19.
public class Client {
private Client(){};
public static void main(String[] args){
//創(chuàng)建WebService客戶端代理工廠
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
//注冊WebService接口
factory.setServiceClass(HelloWorld.class);
//設(shè)置WebService地址
factory.setAddress("http://localhost:9000/HelloWorld");
IHelloWorld iHelloWorld = (IHelloWorld)factory.create();
System.out.println("invoke webservice...");
System.out.println("message context is:"+iHelloWorld.sayHi("
Josen"));
System.exit(0);
}
}
最后是萬事俱備,只欠測試了
首先,運行服務(wù)端程序
其次,打開瀏覽器,在地址欄中輸入http://localhost:9000/HelloWorld?wsdl(因為cxf自帶了一個jetty服務(wù)器),查看接口是否發(fā)布成功,如里瀏覽器頁面顯示下面內(nèi)容,證明接口發(fā)布成功
本文轉(zhuǎn)自http://xuzhfa123.javaeye.com/blog/563302,特別此說明
首先到apache官方網(wǎng)下載apache-cxf-2.2.2,地址:http://cxf.apache.org/
新建一個Java Project,導(dǎo)入cxf常用.jar包
Java代碼
1.commons-logging-1.1.1.jar
2.geronimo-activation_1.1_spec-1.0.2.jar (or Sun's Activation jar)
3.geronimo-annotation_1.0_spec-1.1.1.jar (JSR 250)
4.geronimo-javamail_1.4_spec-1.6.jar (or Sun's JavaMail jar)
5.geronimo-servlet_2.5_spec-1.2.jar (or Sun's Servlet jar)
6.geronimo-ws-metadata_2.0_spec-1.1.2.jar (JSR 181)
7.geronimo-jaxws_2.1_spec-1.0.jar (or Sun's jaxws-api-2.1.jar)
8.geronimo-stax-api_1.0_spec-1.0.1.jar (or other stax-api jar)
9.jaxb-api-2.1.jar
10.jaxb-impl-2.1.12.jar
11.jetty-6.1.21.jar
12.jetty-util-6.1.21.jar
13.neethi-2.0.4.jar
14.saaj-api-1.3.jar
15.saaj-impl-1.3.2.jar
16.wsdl4j-1.6.2.jar
17.wstx-asl-3.2.8.jar
18.XmlSchema-1.4.5.jar
19.xml-resolver-1.2.jar
20.cxf-2.2.2.jar
commons-logging-1.1.1.jar
geronimo-activation_1.1_spec-1.0.2.jar (or Sun's Activation jar)
geronimo-annotation_1.0_spec-1.1.1.jar (JSR 250)
geronimo-javamail_1.4_spec-1.6.jar (or Sun's JavaMail jar)
geronimo-servlet_2.5_spec-1.2.jar (or Sun's Servlet jar)
geronimo-ws-metadata_2.0_spec-1.1.2.jar (JSR 181)
geronimo-jaxws_2.1_spec-1.0.jar (or Sun's jaxws-api-2.1.jar)
geronimo-stax-api_1.0_spec-1.0.1.jar (or other stax-api jar)
jaxb-api-2.1.jar
jaxb-impl-2.1.12.jar
jetty-6.1.21.jar
jetty-util-6.1.21.jar
neethi-2.0.4.jar
saaj-api-1.3.jar
saaj-impl-1.3.2.jar
wsdl4j-1.6.2.jar
wstx-asl-3.2.8.jar
XmlSchema-1.4.5.jar
xml-resolver-1.2.jar
cxf-2.2.2.jar
接下就是HelloWorld Demo開發(fā)了
第一步:新建一個webservice接口
Java代碼
1.@WebService
2.public interface IHelloWorld {
3. //@WebParam給參數(shù)命名,提高可代碼可讀性。此項可選
4.blic String sayHi(@WebParam(name="text") String text);
5.}
@WebService
public interface IHelloWorld {
//@WebParam給參數(shù)命名,提高可代碼可讀性。此項可選
public String sayHi(@WebParam(name="text") String text);
}
通過注解@WebService申明為webservice接口
第二步,實現(xiàn)WebService接口
Java代碼
1. @WebService
2. public class HelloWorldImpl implements IHelloWorld {
3.
4.public String sayHi(String name) {
5. System.out.println("sayHello is called by " + name);
6. return "Hello " + name;
7.}
8.
9. }
@WebService
public class HelloWorldImpl implements IHelloWorld {
public String sayHi(String name) {
System.out.println("sayHello is called by " + name);
return "Hello " + name;
}
}
第三步,創(chuàng)建服務(wù)端
Java代碼
1. public class Server {
2.
3.private Server(){
4. IHelloWorld helloWorld = new HelloWorldImpl();
5. //創(chuàng)建WebService服務(wù)工廠
6. JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
7. //注冊WebService接口
8. factory.setServiceClass(IHelloWorld.class);
9. //發(fā)布接口
10. factory.setAddress("http://localhost:9000/HelloWorld");
11. factory.setServiceBean(helloWorld);
12. //創(chuàng)建WebService
13. factory.create();
14.};
15.
16.public static void main(String[] args) throws InterruptedException{
17. //啟動服務(wù)端
18. new Server();
19. System.out.println("Server ready...");
20. //休眠一分鐘,便于測試
21. Thread.sleep(1000*60);
22. System.out.println("Server exit...");
23. System.exit(0);
24.}
25. }
public class Server {
private Server(){
IHelloWorld helloWorld = new HelloWorldImpl();
//創(chuàng)建WebService服務(wù)工廠
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
//注冊WebService接口
factory.setServiceClass(IHelloWorld.class);
//發(fā)布接口
factory.setAddress("http://localhost:9000/HelloWorld");
factory.setServiceBean(helloWorld);
//創(chuàng)建WebService
factory.create();
};
public static void main(String[] args) throws InterruptedException{
//啟動服務(wù)端
new Server();
System.out.println("Server ready...");
//休眠一分鐘,便于測試
Thread.sleep(1000*60);
System.out.println("Server exit...");
System.exit(0);
}
}
第四步,創(chuàng)建客戶端
Java代碼
1. public class Client {
2.
3.private Client(){};
4.
5.public static void main(String[] args){
6. //創(chuàng)建WebService客戶端代理工廠
7. JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
8. //注冊WebService接口
9. factory.setServiceClass(HelloWorld.class);
10. //設(shè)置WebService地址
11. factory.setAddress("http://localhost:9000/HelloWorld");
12. IHelloWorld iHelloWorld = (IHelloWorld)factory.create();
13. System.out.println("invoke webservice...");
14. System.out.println("message context is:"+iHelloWorld.sayHi("
15. Josen"));
16. System.exit(0);
17.}
18. }
19.
public class Client {
private Client(){};
public static void main(String[] args){
//創(chuàng)建WebService客戶端代理工廠
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
//注冊WebService接口
factory.setServiceClass(HelloWorld.class);
//設(shè)置WebService地址
factory.setAddress("http://localhost:9000/HelloWorld");
IHelloWorld iHelloWorld = (IHelloWorld)factory.create();
System.out.println("invoke webservice...");
System.out.println("message context is:"+iHelloWorld.sayHi("
Josen"));
System.exit(0);
}
}
最后是萬事俱備,只欠測試了
首先,運行服務(wù)端程序
其次,打開瀏覽器,在地址欄中輸入http://localhost:9000/HelloWorld?wsdl(因為cxf自帶了一個jetty服務(wù)器),查看接口是否發(fā)布成功,如里瀏覽器頁面顯示下面內(nèi)容,證明接口發(fā)布成功
本文轉(zhuǎn)自http://xuzhfa123.javaeye.com/blog/563302,特別此說明