Eclipse+Maven+Spring+CXF 構(gòu)建webservice 服務(wù)
一、 軟件準(zhǔn)備
- Eclipse 4.2.1
- Maven 2.2.1
- Spring 3.2.6
- CXF 3.0.2
二、 步驟
1. 新建web工程,利用maven管理,如下:
工程名為test,完成以后,項目結(jié)構(gòu)如下圖:
src/main/java 準(zhǔn)備放 java 程序;
src/main/resources準(zhǔn)備放各類資源文件。
2. 添加代碼
1) 定義服務(wù)接口
- ackage com.test;
- import javax.jws.WebService;
- @WebService
- public interface HelloWorld {
- public String sayHello();
- }
因為只是一個webservice的實驗程序,所以非常簡單,只有一個服務(wù)方法: sayHello(),利用 @WebService注解來聲明這是一個webservice的接口。
2) 實現(xiàn)服務(wù)類
- package com.test;
- import javax.jws.WebService;
- @WebService
- public class HelloWorldImpl implements HelloWorld{
- public String sayHello(){
- return "Hello world!";
- }
- }
完成java代碼添加后的項目結(jié)構(gòu)如下:
3. 添加Spring-CXF 配置
在項目 src/main/webapp/WEB-INF 目錄下新建XML定義:cxf-servlet.xml如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <!--
- Licensed to the Apache Software Foundation (ASF) under one
- or more contributor license agreements. See the NOTICE file
- distributed with this work for additional information
- regarding copyright ownership. The ASF licenses this file
- to you under the Apache License, Version 2.0 (the
- "License"); you may not use this file except in compliance
- with the License. You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing,
- software distributed under the License is distributed on an
- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- KIND, either express or implied. See the License for the
- specific language governing permissions and limitations
- under the License.
- -->
- <!-- START SNIPPET: beans -->
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:jaxws="http://cxf.apache.org/jaxws"
- xsi:schemaLocation=" http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
- <import resource="classpath:META-INF/cxf/cxf.xml"/>
- <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
- <jaxws:endpoint id="helloWorld" implementor="com.test.HelloWorldImpl" address="/HelloWorld"/>
- </beans>
- <!-- END SNIPPET: beans -->
該定義文件利用spring和CXF的功能,發(fā)布一個ID為helloWorld,實現(xiàn)類為com.test.HelloWorldImpl,發(fā)布 相對路徑為 /HelloWorld(對應(yīng)絕對目錄為: http://host:port/{WebAPPName}/HelloWorld)的 webservice。 因為我們需要用到CXF來做webservice,右鍵點擊項目中的POM.XML,添加apache-cxf依賴。
4. Web應(yīng)用配置
修改 src/main/webapp/WEB-INF 目錄下的web.xml文件
- <?xml version="1.0" encoding="UTF-8"?>
- <!--
- Licensed to the Apache Software Foundation (ASF) under one
- or more contributor license agreements. See the NOTICE file
- distributed with this work for additional information
- regarding copyright ownership. The ASF licenses this file
- to you under the Apache License, Version 2.0 (the
- "License"); you may not use this file except in compliance
- with the License. You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing,
- software distributed under the License is distributed on an
- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- KIND, either express or implied. See the License for the
- specific language governing permissions and limitations
- under the License.
- -->
- <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <display-name>cxf</display-name>
- <servlet>
- <description>Apache CXF Endpoint</description>
- <display-name>cxf</display-name>
- <servlet-name>cxf</servlet-name>
- <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>cxf</servlet-name>
- <url-pattern>/*</url-pattern>
- </servlet-mapping>
- <session-config>
- <session-timeout>60</session-timeout>
- </session-config>
- </web-app>
該文件實際上是定義了處理webservice的CXF Servlet的映射關(guān)系。
完成步驟3和4以后的工程目錄如下:
5. 編譯打包
利用maven(package -X)編譯打包成test.war
(在Eclipse上右擊工程名 Run as -> Maven build)
6. 將步驟5生成的test.war部署到tomcat服務(wù)器上
7. 訪問測試:
在瀏覽器上輸入:http://localhost:8080/test/,出現(xiàn)如下畫面就成功了:
點擊WSDL鏈接:
8. 編寫webservice client端代碼
1) 首先通過 Spring 與 CXF 的配置來定義 Web Service 的客戶端 Bean,在 src\main\resources 目錄下創(chuàng)建client-beans.xml 配置文件:
- <?xml version="1.0" encoding="UTF-8"?>
- <!--
- Licensed to the Apache Software Foundation (ASF) under one
- or more contributor license agreements. See the NOTICE file
- distributed with this work for additional information
- regarding copyright ownership. The ASF licenses this file
- to you under the Apache License, Version 2.0 (the
- "License"); you may not use this file except in compliance
- with the License. You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing,
- software distributed under the License is distributed on an
- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- KIND, either express or implied. See the License for the
- specific language governing permissions and limitations
- under the License.
- -->
- <!-- START SNIPPET: beans -->
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:jaxws="http://cxf.apache.org/jaxws"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://cxf.apache.org/jaxws
- http://cxf.apache.org/schema/jaxws.xsd">
- <bean id="client" class="com.test.HelloWorld"
- factory-bean="clientFactory" factory-method="create"/>
- <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
- <property name="serviceClass" value="com.test.HelloWorld"/>
- <property name="address" value="http://localhost:8080/test/HelloWorld"/>
- </bean>
- </beans>
- <!-- END SNIPPET: beans -->
需要注意的是,該配置文件中的 address需要寫成發(fā)布服務(wù)的絕對路徑。
2) 編寫客戶端java代碼: HelloWorldClient.java
- package com.test;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public final class HelloWorldClient {
- private HelloWorldClient() {
- }
- public static void main(String args[]) throws Exception {
- // START SNIPPET: client
- ClassPathXmlApplicationContext context
- = new ClassPathXmlApplicationContext(new String[] {"client-beans.xml"});
- HelloWorld client = (HelloWorld)context.getBean("client");
- String response = client.sayHello();
- System.out.println("Response: " + response);
- System.exit(0);
- // END SNIPPET: client
- }
- }
注意,代碼中HelloWorldclient = (HelloWorld)context.getBean("client"); 的client需要與"client-beans.xml"中的 bean id一致才能找到這個服務(wù)。
現(xiàn)在的項目結(jié)構(gòu)如下:
3)連接測試
在eclipse中直接按HelloWorldClient運行 Run as -> Java Application:
輸出的Hello world! 即是我們發(fā)布的HelloWorld的方法 sayHello()的輸出!這說明從服務(wù)發(fā)布到客戶端連接都成功了。