下載apache-cxf-3.1.8,創建java web項目,將cxf包的lib下的所有jar包放到項目中,並去掉下圖的幾個包:
如果你保留了而且沒有報錯,那麼恭喜你。
將下載的cxf包的samples\java_first_spring_support下的文件夾直接複製到自己創建的項目,如圖:
java文件夾的內容是服務端和客戶端代碼,resources中是spring配置文件,webapp中是web.xml和cxf-servlet.xml,
如圖,我 將java文件夾中的內容直接放到src下,將resources中的client-beans.xml拿出來放在src下了,
把web.xml和cxf-servlet.xml放到WEB-INF下了。 後來我將cxf-servlet.xml去掉也一樣運行。
啓動服務的Server.java按照案例中的代碼總是報錯,應該少包,所以我就換成下面這樣:
protected Server() throws Exception {
System.out.println("Starting Server");
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
// 註冊WebService接口
factory.setServiceClass(HelloWorld.class);
// 發佈接口
factory.setAddress("http://localhost:9002/HelloWorld");
factory.setServiceBean(new HelloWorldImpl());
// factory.getInInterceptors().add(new LoggingInInterceptor());
// factory.getOutInterceptors().add(new LoggingOutInterceptor());
// 創建WebService
factory.create();
}
public static void main(String args[]) throws Exception {
new Server();
}
其他都不用變了,只要將配置文件中的路徑改成你項目中正確的路徑即可。
先執行Server.java啓動服務,再執行Client.java,輸出:
總結:使用spring的地方就是將服務和客戶端都配置到spring配置文件中,目前只領會這一點,至於cxf-servlet.xml
還不知道有什麼用,因爲有沒有都不會報錯。