Eclipse+Maven+Spring+CXF 构建webservice 服务

1、   软件准备

Eclipse 4.2.1html

Maven 2.2.1java

Spring 3.2.6web

CXF 3.0.2spring

软件下载和Eclipse 安装 maven插件等请参考其余文章。express

2、   步骤

1.        新建web工程,利用maven管理,以下:apache

 

  

工程名为test,完成之后,项目结构以下图:浏览器

src/main/java 准备放 java 程序;tomcat

src/main/resources准备放各种资源文件。服务器

2.        添加代码session

1)        定义服务接口

 

[java]  view plain copy 在CODE上查看代码片 派生到个人代码片
 
  1. package com.test;  
  2.    
  3. import javax.jws.WebService;  
  4.    
  5. @WebService  
  6. public interface HelloWorld {  
  7.     public String sayHello();  
  8. }  

 

由于只是一个webservice的实验程序,因此很是简单,只有一个服务方法: sayHello(),利用 @WebService注解来声明这是一个webservice的接口。

 

2)        实现服务类

 

[java]  view plain copy 在CODE上查看代码片 派生到个人代码片
 
  1. package com.test;  
  2.    
  3. import javax.jws.WebService;  
  4.    
  5. @WebService  
  6. public class HelloWorldImpl implements HelloWorld{  
  7.      
  8.     public String sayHello(){  
  9.         return "Hello world!";  
  10.     }  
  11. }  

 

 

完成java代码添加后的项目结构以下:

 

 

3.        添加Spring-CXF 配置

在项目 src/main/webapp/WEB-INF 目录下新建XML定义:cxf-servlet.xml以下:

 

[html]  view plain copy 在CODE上查看代码片 派生到个人代码片
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!--  
  3.         Licensed to the Apache Software Foundation (ASF) under one  
  4.         or more contributor license agreements. See the NOTICE file  
  5.         distributed with this work for additional information  
  6.         regarding copyright ownership. The ASF licenses this file  
  7.         to you under the Apache License, Version 2.0 (the  
  8.         "License"); you may not use this file except in compliance  
  9.         with the License. You may obtain a copy of the License at  
  10.          
  11.         http://www.apache.org/licenses/LICENSE-2.0  
  12.          
  13.         Unless required by applicable law or agreed to in writing,  
  14.         software distributed under the License is distributed on an  
  15.         "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY  
  16.         KIND, either express or implied. See the License for the  
  17.         specific language governing permissions and limitations  
  18.         under the License.  
  19. -->  
  20. <!-- START SNIPPET: beans -->  
  21. <beans xmlns="http://www.springframework.org/schema/beans"  
  22.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  23.     xmlns:jaxws="http://cxf.apache.org/jaxws"  
  24.     xsi:schemaLocation=" http://www.springframework.org/schema/beans  
  25.     http://www.springframework.org/schema/beans/spring-beans.xsd  
  26.     http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">  
  27.     <import resource="classpath:META-INF/cxf/cxf.xml"/>  
  28.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>  
  29.     <jaxws:endpoint id="helloWorld" implementor="com.test.HelloWorldImpl" address="/HelloWorld"/>  
  30. </beans>  
  31. <!-- END SNIPPET: beans -->  

 

该定义文件利用spring和CXF的功能,发布一个ID为helloWorld,实现类为com.test.HelloWorldImpl,发布相对路径为 /HelloWorld(对应绝对目录为: http://host:port/{WebAPPName}/HelloWorld)的 webservice。

 由于咱们须要用到CXF来作webservice,右键点击项目中的POM.XML,添加apache-cxf依赖,以下图:

 

4.        Web应用配置

修改 src/main/webapp/WEB-INF 目录下的web.xml文件

 

[html]  view plain copy 在CODE上查看代码片 派生到个人代码片
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!--  
  3.   Licensed to the Apache Software Foundation (ASF) under one  
  4.   or more contributor license agreements. See the NOTICE file  
  5.   distributed with this work for additional information  
  6.   regarding copyright ownership. The ASF licenses this file  
  7.   to you under the Apache License, Version 2.0 (the  
  8.   "License"); you may not use this file except in compliance  
  9.   with the License. You may obtain a copy of the License at  
  10.    
  11.   http://www.apache.org/licenses/LICENSE-2.0  
  12.    
  13.   Unless required by applicable law or agreed to in writing,  
  14.   software distributed under the License is distributed on an  
  15.   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY  
  16.   KIND, either express or implied. See the License for the  
  17.   specific language governing permissions and limitations  
  18.   under the License.  
  19. -->  
  20. <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">  
  21.     <display-name>cxf</display-name>  
  22.     <servlet>  
  23.         <description>Apache CXF Endpoint</description>  
  24.         <display-name>cxf</display-name>  
  25.         <servlet-name>cxf</servlet-name>  
  26.         <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
  27.         <load-on-startup>1</load-on-startup>  
  28.     </servlet>  
  29.     <servlet-mapping>  
  30.         <servlet-name>cxf</servlet-name>  
  31.         <url-pattern>/*</url-pattern>  
  32.     </servlet-mapping>  
  33.     <session-config>  
  34.         <session-timeout>60</session-timeout>  
  35.     </session-config>  
  36. </web-app>  

 

该文件其实是定义了处理webservice的CXF Servlet的映射关系。

 完成步骤3和4之后的工程目录以下:

 

5.        编译打包

利用maven(package -X)编译打包成test.war

(在Eclipse上右击工程名 Run as -> Maven build)

6.        将步骤5生成的test.war部署到tomcat服务器上

7.        访问测试:

在浏览器上输入:http://localhost:8080/test/,出现以下画面就成功了:

 

点击WSDL连接:

 

8.        编写webservice client端代码

1)        首先经过 Spring 与 CXF 的配置来定义 Web Service 的客户端 Bean,在 src\main\resources 目录下建立client-beans.xml 配置文件:

 

[html]  view plain copy 在CODE上查看代码片 派生到个人代码片
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!--  
  3.         Licensed to the Apache Software Foundation (ASF) under one  
  4.         or more contributor license agreements. See the NOTICE file  
  5.         distributed with this work for additional information  
  6.         regarding copyright ownership. The ASF licenses this file  
  7.         to you under the Apache License, Version 2.0 (the  
  8.         "License"); you may not use this file except in compliance  
  9.         with the License. You may obtain a copy of the License at  
  10.          
  11.         http://www.apache.org/licenses/LICENSE-2.0  
  12.          
  13.         Unless required by applicable law or agreed to in writing,  
  14.         software distributed under the License is distributed on an  
  15.         "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY  
  16.         KIND, either express or implied. See the License for the  
  17.         specific language governing permissions and limitations  
  18.         under the License.  
  19. -->  
  20. <!-- START SNIPPET: beans -->  
  21. <beans xmlns="http://www.springframework.org/schema/beans"  
  22.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  23.     xmlns:jaxws="http://cxf.apache.org/jaxws"  
  24.     xsi:schemaLocation="  
  25.     http://www.springframework.org/schema/beans  
  26.     http://www.springframework.org/schema/beans/spring-beans.xsd  
  27.     http://cxf.apache.org/jaxws  
  28.      http://cxf.apache.org/schema/jaxws.xsd">  
  29.     <bean id="client" class="com.test.HelloWorld"  
  30.         factory-bean="clientFactory" factory-method="create"/>  
  31.     <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">  
  32.         <property name="serviceClass" value="com.test.HelloWorld"/>  
  33.         <property name="address" value="http://localhost:8080/test/HelloWorld"/>  
  34.     </bean>  
  35. </beans>  
  36. <!-- END SNIPPET: beans -->  

 

须要注意的是,该配置文件中的 address须要写成发布服务的绝对路径。

 

2)        编写客户端java代码: HelloWorldClient.java

 

[java]  view plain copy 在CODE上查看代码片 派生到个人代码片
 
  1. package com.test;  
  2.    
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  4.    
  5. public final class HelloWorldClient {  
  6.    
  7.     private HelloWorldClient() {  
  8.     }  
  9.    
  10.     public static void main(String args[]) throws Exception {  
  11.         // START SNIPPET: client  
  12.         ClassPathXmlApplicationContext context  
  13.             = new ClassPathXmlApplicationContext(new String[] {"client-beans.xml"});  
  14.    
  15.         HelloWorld client = (HelloWorld)context.getBean("client");  
  16.    
  17.         String response = client.sayHello();  
  18.         System.out.println("Response: " + response);  
  19.         System.exit(0);  
  20.         // END SNIPPET: client  
  21.     }  
  22. }  

 

注意,代码中HelloWorldclient = (HelloWorld)context.getBean("client"); 的client须要与"client-beans.xml"中的 bean id一致才能找到这个服务。 

如今的项目结构以下:

 

3)        链接测试

在eclipse中直接按HelloWorldClient运行 Run as -> Java Application: 

 输出的Hello world! 便是咱们发布的HelloWorld的方法 sayHello()的输出!这说明从服务发布到客户端链接都成功了。

相关文章
相关标签/搜索