webservices——發佈CXF的方法總結!!!

注意:jar包還是*.jar哦!!!

 

先說發佈的方法總結!

 

第一種:用一個j2se的main方法來發布

 

  1. public class Server {  
  2.     public static void main(String[] args) {  
  3.         Endpoint.publish("http://127.0.0.1:8080/cxf"new HelloImpl());  
  4.     }  
  5. }  

 

第二種用tomcat來發布

webX.xml:

  1. <servlet>  
  2.     <servlet-name>CXFServlet</servlet-name>  
  3.     <servlet-class>t.T</servlet-class>  
  4.     <init-param>  
  5.         <param-name>/hello</param-name>  
  6.         <param-value>t.HelloImpl</param-value>  
  7.     </init-param>  
  8.     <load-on-startup>1</load-on-startup>  
  9. </servlet>  
  10. <servlet-mapping>  
  11.     <servlet-name>CXFServlet</servlet-name>  
  12.     <url-pattern>/services/*</url-pattern>  
  13. </servlet-mapping>  

 

t.java:

  1. package t;  
  2.   
  3. import java.util.Enumeration;  
  4.   
  5. import javax.servlet.ServletConfig;  
  6. import javax.servlet.ServletException;  
  7. import javax.xml.ws.Endpoint;  
  8.   
  9. import org.apache.cxf.Bus;  
  10. import org.apache.cxf.BusFactory;  
  11. import org.apache.cxf.transport.servlet.CXFNonSpringServlet;  
  12.   
  13. public class T extends CXFNonSpringServlet {  
  14.   
  15.     private static final long serialVersionUID = -4143021604478775522L;  
  16.   
  17.     public void loadBus(ServletConfig servletConfig) throws ServletException {  
  18.         super.loadBus(servletConfig);  
  19.         Bus bus = this.getBus();  
  20.         BusFactory.setDefaultBus(bus);  
  21.         // 獲取在web.xml中配置的要發佈的所有的Web服務實現類併發布Web服務  
  22.         Enumeration<String> enumeration = this.getInitParameterNames();  
  23.         while (enumeration.hasMoreElements()) {  
  24.             String key = enumeration.nextElement();  
  25.             String value = this.getInitParameter(key);  
  26.             try {  
  27.                 Class clazz = Class.forName(value);  
  28.                 try {  
  29.                     Endpoint.publish(key, clazz.newInstance());  
  30.                 } catch (InstantiationException e) {  
  31.                     e.printStackTrace();  
  32.                 } catch (IllegalAccessException e) {  
  33.                     e.printStackTrace();  
  34.                 }  
  35.             } catch (ClassNotFoundException e) {  
  36.                 e.printStackTrace();  
  37.             }  
  38.         }  
  39.     }  
  40.   
  41. }  

 

第三種:還是用tomcat,但是採用spring的一些東東。

web.xml

  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>org.springframework.web.context.ContextLoaderListener</listener-class>  
  7. </listener>  
  8. <servlet>  
  9.     <servlet-name>CXFServlet</servlet-name>  
  10.     <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
  11.     <load-on-startup>1</load-on-startup>  
  12. </servlet>  
  13. <servlet-mapping>  
  14.     <servlet-name>CXFServlet</servlet-name>  
  15.     <url-pattern>/services/*</url-pattern>  
  16. </servlet-mapping>  

 

同目錄還有一個beans.cml:

  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.     xmlns:jaxrs="http://cxf.apache.org/jaxrs"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7.     http://www.springframework.org/schema/beans/spring-beans.xsd  
  8.     http://cxf.apache.org/jaxws  
  9.     http://cxf.apache.org/schemas/jaxws.xsd  
  10.     http://cxf.apache.org/jaxrs  
  11.     http://cxf.apache.org/schemas/jaxrs.xsd">  
  12.       
  13.     <import resource="classpath:META-INF/cxf/cxf.xml" />  
  14.     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>  
  15.     <import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml" />  
  16.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
  17.       
  18.     <jaxws:endpoint id="helloServiceWs" address="/hello" implementor="#helloService" />  
  19.     <bean id="helloService" class="t.HelloImpl" />  
  20.       
  21. </beans>  

 

再說一哈,客戶端代碼生成總結!!!這裏提一下而已,多個思路。

 

1.手寫:

  1. public class Client {  
  2.     public static void main(String[] args) {  
  3.         JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();  
  4.         factory.setAddress("http://127.0.0.1/cxf_server/services/hello");  
  5.         factory.setServiceClass(HelloInter.class);  
  6.         HelloInter helloInter = (HelloInter) factory.create();  
  7.         helloInter.hello();  
  8.     }  
  9. }  

 

2.用eclipse自動生成!

 

3.用XFire裏面有的東西也可以生成。

 

4.用spring也可以生成。

 

5.用圖形界面工具,比如soapui。(最簡單)

 

6.用ant自動生成等等。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章