Eclipse安装CXF插件开发java web service 集成Spring

本文主要介绍在Eclipse[3.3.2]安装CXF插件。开发一个简单的java web service,以及和Spring的集成。
安装插件:
1,下载STP all_in_one,从http://ftp.daum.net/eclipse/stp/old-downloads-page/可以下载stp-all-in-one-incubation-0.7.0.200711162004.zip
安装这个插件,可以用link文件的方式安装,可参考 http://blog.csdn.net/kkdelta/archive/2009/03/12/3983999.aspx
2,下载CXF 运行环境所需的Jar包,
http://people.apache.org/repo/m2-snapshot-repository/org/apache/cxf/apache-cxf/
我用的是 apache-cxf-2.1-incubator-20080414.232258-49.zip
3,打开eclipse后,在菜单栏,windows-->preference-->soa tools 如下图,说明插件安装成功。


4,配置CXF运行环境,如下图,installed Runtimes---- Add--Appach CXF 2.0--Next 指定解压缩后的apache-cxf-2.1-incubator-20080414.232258-49.zip的路径。

开发Web Java Service
1,新建一个web project,在 这个project里建立下面的interface:
package com.test.cxf;
public interface WSprovider {
    public String testWS(String msgIn);
}
然后在这个建好后的project上点右键,JAX-WS Tools ---Enable JAX-WS --java first programing mode, 选择运行cxf环境-- 选择新建的interface--finish。
你的interface将被加上Java anotation如下:
[java]  view plain copy
  1. package com.test.cxf;  
  2. import javax.jws.WebService;  
  3. @WebService(name="WSprovider", targetNamespace="http://cxf.test.com/")  
  4. public interface WSprovider {  
  5.     public String testWS(String msgIn);  
  6. }  
[java]  view plain  copy
  1. package com.test.cxf;  
  2. import javax.jws.WebService;  
  3. @WebService(name="WSprovider", targetNamespace="http://cxf.test.com/")  
  4. public interface WSprovider {  
  5.     public String testWS(String msgIn);  
  6. }  

2,在outline视图,选中testws(),右键选JAX-WX tools--〉create web method
你的interface将被加上Java anotation如下:
[java]  view plain copy
  1. @WebService(name="WSprovider", targetNamespace="http://cxf.test.com/")  
  2. public interface WSprovider {  
  3.     @WebMethod(operationName="testWS", exclude=false)  
  4.     @ResponseWrapper(className="com.test.cxf.TestWSResponse", localName="testWSResponse", targetNamespace="http://cxf.test.com/")  
  5.     @RequestWrapper(className="com.test.cxf.TestWS", localName="testWS", targetNamespace="http://cxf.test.com/")  
  6.     public String testWS(String msgIn);  
  7. }  
[java]  view plain  copy
  1. @WebService(name="WSprovider", targetNamespace="http://cxf.test.com/")  
  2. public interface WSprovider {  
  3.     @WebMethod(operationName="testWS", exclude=false)  
  4.     @ResponseWrapper(className="com.test.cxf.TestWSResponse", localName="testWSResponse", targetNamespace="http://cxf.test.com/")  
  5.     @RequestWrapper(className="com.test.cxf.TestWS", localName="testWS", targetNamespace="http://cxf.test.com/")  
  6.     public String testWS(String msgIn);  
  7. }  
3,然后在project上点右键,JAX-WS Tools ---Generate All
会生成interface的实现类如下:

[java]  view plain copy
  1. @WebService(endpointInterface = "com.test.cxf.WSprovider", serviceName = "WSproviderService")                       
  2. public class WSproviderImpl implements WSprovider {  
  3.     public java.lang.String testWS(java.lang.String arg0) {   
  4.         ........  
  5.     }  
  6. }  
[java]  view plain  copy
  1. @WebService(endpointInterface = "com.test.cxf.WSprovider", serviceName = "WSproviderService")                       
  2. public class WSproviderImpl implements WSprovider {  
  3.     public java.lang.String testWS(java.lang.String arg0) {   
  4.         ........  
  5.     }  
  6. }  

到此,简单的web service的开发就算完成了。
集成Spring
1,在WEB-INF下建立一个bean.xml文件:

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:jaxws="http://cxf.apache.org/jaxws"  
  5.     xsi:schemaLocation="  
  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  7. http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">  
  8.   
  9.     <import resource="classpath:META-INF/cxf/cxf.xml" />  
  10.     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
  11.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
  12.   
  13.     <bean name="testService" class="com.test.cxf.WSCXFProviderImpl"/>  
  14.      
  15.     <jaxws:endpoint  
  16.         id="testEndpoint"  
  17.         implementor="#testService"  
  18.         wsdlLocation="classpath:wsdl/prjCXFWeb.wsdl"  
  19.         address="/WSCXFProviderPort">  
  20.         <jaxws:features>  
  21.             <bean class="org.apache.cxf.feature.LoggingFeature"/>  
  22.         </jaxws:features>  
  23.     </jaxws:endpoint>       
  24. </beans>  
[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:jaxws="http://cxf.apache.org/jaxws"  
  5.     xsi:schemaLocation="  
  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  7. http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">  
  8.   
  9.     <import resource="classpath:META-INF/cxf/cxf.xml" />  
  10.     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
  11.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
  12.   
  13.     <bean name="testService" class="com.test.cxf.WSCXFProviderImpl"/>  
  14.      
  15.     <jaxws:endpoint  
  16.         id="testEndpoint"  
  17.         implementor="#testService"  
  18.         wsdlLocation="classpath:wsdl/prjCXFWeb.wsdl"  
  19.         address="/WSCXFProviderPort">  
  20.         <jaxws:features>  
  21.             <bean class="org.apache.cxf.feature.LoggingFeature"/>  
  22.         </jaxws:features>  
  23.     </jaxws:endpoint>       
  24. </beans>  

2,对生成的wsdl文件修改:
把<soap:address location="http://localhost:9090/WSproviderPort"/>
改成<soap:address location="http://localhost:8080/prjCXFWeb/services/WSCXFProviderPort" />
在你的src下新建一个wsdl文件,把改后的wsdl copy到此【为了对应bean.xml中的wsdlLocation】。

3,在web.xml中加入:
[html]  view plain copy
  1. <context-param>  
  2.       <param-name>contextConfigLocation</param-name>  
  3.       <param-value>WEB-INF/beans.xml</param-value>  
  4.   </context-param>  
  5.   <listener>  
  6.       <listener-class>  
  7.           org.springframework.web.context.ContextLoaderListener  
  8.       </listener-class>  
  9.   </listener>  
  10.   <servlet>  
  11.       <servlet-name>CXFServlet</servlet-name>  
  12.       <servlet-class>  
  13.           org.apache.cxf.transport.servlet.CXFServlet  
  14.       </servlet-class>  
  15.       <load-on-startup>1</load-on-startup>  
  16.   </servlet>  
  17.   <servlet-mapping>  
  18.       <servlet-name>CXFServlet</servlet-name>  
  19.       <url-pattern>/services/*</url-pattern>  
  20.   </servlet-mapping>  
[html]  view plain  copy
  1. <context-param>  
  2.       <param-name>contextConfigLocation</param-name>  
  3.       <param-value>WEB-INF/beans.xml</param-value>  
  4.   </context-param>  
  5.   <listener>  
  6.       <listener-class>  
  7.           org.springframework.web.context.ContextLoaderListener  
  8.       </listener-class>  
  9.   </listener>  
  10.   <servlet>  
  11.       <servlet-name>CXFServlet</servlet-name>  
  12.       <servlet-class>  
  13.           org.apache.cxf.transport.servlet.CXFServlet  
  14.       </servlet-class>  
  15.       <load-on-startup>1</load-on-startup>  
  16.   </servlet>  
  17.   <servlet-mapping>  
  18.       <servlet-name>CXFServlet</servlet-name>  
  19.       <url-pattern>/services/*</url-pattern>  
  20.   </servlet-mapping>  
4,将web project发布到web container(e.g tomcat)中,web service便可以被调用了。
在IE中输入 http://localhost:8080/prjCXFWeb/services/WSCXFProviderPort?wsdl,能看到wsdl文件,证明
web service发布成功了。

如果不和Spring集成,可以自己实现一个CXFNonSpringServlet的servlet,在web.xml中配置这个servlet来处理web service的请求.
[html]  view plain copy
  1. public class SimpleServlet extends CXFNonSpringServlet {  
  2.     private static final long serialVersionUID = 1L;  
  3.   
  4.     public void loadBus(ServletConfig servletConfig) throws ServletException {  
  5.         super.loadBus(servletConfig);  
  6.         BusFactory.setDefaultBus(getBus());  
  7.         Object implementor = new WSCXFProviderImpl();  
  8.         Endpoint.publish("/p1", implementor);  
  9.     }  
  10. }  
  11.   
  12.     <!-- not using Spring -->  
  13.     <servlet>  
  14.         <servlet-name>CXFServlet</servlet-name>  
  15.         <servlet-class>  
  16.             com.bt.cxf.ws.SimpleServlet  
  17.         </servlet-class>  
  18.         <load-on-startup>1</load-on-startup>  
  19.     </servlet>  
  20.   
  21.     <servlet-mapping>  
  22.         <servlet-name>CXFServlet</servlet-name>  
  23.         <url-pattern>/services/*</url-pattern>  
  24.     </servlet-mapping>  
[html]  view plain  copy
  1. public class SimpleServlet extends CXFNonSpringServlet {  
  2.     private static final long serialVersionUID = 1L;  
  3.   
  4.     public void loadBus(ServletConfig servletConfig) throws ServletException {  
  5.         super.loadBus(servletConfig);  
  6.         BusFactory.setDefaultBus(getBus());  
  7.         Object implementor = new WSCXFProviderImpl();  
  8.         Endpoint.publish("/p1", implementor);  
  9.     }  
  10. }  
  11.   
  12.     <!-- not using Spring -->  
  13.     <servlet>  
  14.         <servlet-name>CXFServlet</servlet-name>  
  15.         <servlet-class>  
  16.             com.bt.cxf.ws.SimpleServlet  
  17.         </servlet-class>  
  18.         <load-on-startup>1</load-on-startup>  
  19.     </servlet>  
  20.   
  21.     <servlet-mapping>  
  22.         <servlet-name>CXFServlet</servlet-name>  
  23.         <url-pattern>/services/*</url-pattern>  
  24.     </servlet-mapping>  
在IE中输入 http://localhost:8080/prjCXFWeb/ services /p1 ?wsdl,能看到wsdl文件,证明

web service发布成功了。[p1对应 ndpoint.publish("/p1", implementor);]

猜你喜欢

转载自blog.csdn.net/weixin_42231507/article/details/80714736