zoukankan      html  css  js  c++  java
  • Head First WebService with CXF

    1. Download apache cxf :  http://people.apache.org/dist/incubator/cxf/2.0.4-incubator/apache-cxf-2.0.4-incubator.zip
        Download Spring:   http://www.springsource.org/download

    2. Server side:
        WEB-INF/web.xml   -- web description
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    3.     <context-param>
    4.     <param-name>contextConfigLocation</param-name>
    5.     <param-value>WEB-INF/spring.xml</param-value>
    6.     </context-param>
    7.     
    8.     <listener>
    9.     <listener-class>
    10.         org.springframework.web.context.ContextLoaderListener
    11.     </listener-class>
    12.     </listener>
    13.     
    14.     <servlet>
    15.     <servlet-name>CXFServlet</servlet-name>
    16.     <servlet-class>
    17.         org.apache.cxf.transport.servlet.CXFServlet
    18.     </servlet-class>
    19.     <load-on-startup>1</load-on-startup>
    20.     </servlet>
    21.     
    22.     
    23.     <servlet-mapping>
    24.     <servlet-name>CXFServlet</servlet-name>
    25.     <url-pattern>/services/*</url-pattern>
    26.     </servlet-mapping>
    27. </web-app>
        WEB-INF/spring.xml 
    1. <?xml version="1.0"?>
    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. <import resource="classpath:META-INF/cxf/cxf.xml" />
    9. <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    10. <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
    11. <bean id="sample" class="jax.ws.test.WebServiceSampleImpl">
    12. </bean>
    13. <jaxws:endpoint id="sampleEndPoint" implementor="#sample" address="/WebServiceSample" />
    14. </beans>

       
        web service interface WebServiceSample.java
    1. package jax.ws.test;
    2. import javax.jws.WebService;
    3. /**
    4.  * 
    5.  * @author kaven
    6.  *
    7.  */
    8. @WebService 
    9. public interface WebServiceSample {
    10.     String say(String hello); 
    11. }
      
         web  service impl WebServiceSampleImpl.java
    1. package jax.ws.test;
    2. import javax.jws.WebService;
    3. @WebService(endpointInterface = "jax.ws.test.WebServiceSample")
    4. public class WebServiceSampleImpl implements WebServiceSample {
    5.     public String say(String hello) {
    6.         System.out.println("Hello " + hello);
    7.         return "Hi buddy " + hello;
    8.     }
    9. }
        ant build file : build.xml
    1. <?xml version="1.0" encoding="ISO-8859-1"?>
    2. <project default="war" basedir=".">
    3.     
    4.     <property name="dir.warroot"  value="build/jaxWsTest.war"/>
    5.     <property name="dir.lib" value="WebContent/WEB-INF/lib" />
    6.     <!-- change this to your deploy directory -->
    7.     <property name="dir.jboss.delopy"   value="/Users/kaven/Kaven/Resource/JBoss/jboss-4.2.3.GA/server/default/deploy"/>
    8.     
    9.     <path id="build.classpath">
    10.         <fileset dir="${dir.lib}">
    11.             <include name="*.jar"/>
    12.         </fileset>
    13.     <pathelement location="dest"/>
    14.   </path>
    15.     
    16.     <target name="clean">
    17.         <delete dir="build"/>
    18.     </target>
    19.     <target name="build" depends="clean">
    20.         <mkdir dir="build"/>
    21.         <mkdir dir="build/classes"/>
    22.         <javac srcdir="src" destdir="build/classes" classpathref="build.classpath">
    23.         </javac>
    24.     </target>
    25.     
    26.     <target name="war" depends="build">
    27.         <mkdir dir="${dir.warroot}"/>
    28.         <mkdir dir="${dir.warroot}/WEB-INF"/>
    29.         <mkdir dir="${dir.warroot}/WEB-INF/classes"/>
    30.         <copy todir="${dir.warroot}/WEB-INF/classes">
    31.             <fileset dir="build/classes"/>
    32.         </copy>
    33.         <copy todir="${dir.warroot}/WEB-INF">
    34.             <fileset dir="WebContent/WEB-INF"/>
    35.         </copy>
    36.     </target>
    37.     
    38.     <target name="deploy" depends="war">
    39.         <copy todir="${dir.jboss.delopy}/jaxWsTest.war">
    40.             <fileset dir="${dir.warroot}">
    41.                 <include name="**/*"/>
    42.             </fileset>
    43.         </copy>         
    44.     </target>
    45. </project>
    run ant deploy, the webservice will be deployed in your web container.

    browser:  http://127.0.0.1:8080/jaxWsTest/services/WebServiceSample?wsdl
    if you see the wsdl file means the webservice is deployed successfully

    3. client side:
       
    There are many ways to access webservice, for instance.
        I.  WSDL2Java generated Client
        II. JAX-WS Proxy
        III. JAX-WS Dispatch APIs
        To make it simple, we use Proxy to invoke webservice
        WebServiceClient.java
    1. package jax.ws.test.client;
    2. import java.net.MalformedURLException;
    3. import java.net.URL;
    4. import javax.xml.namespace.QName;
    5. import javax.xml.ws.Service;
    6. import jax.ws.test.WebServiceSample;
    7. public class WebServiceClient {
    8.     public static void main(String[] args) throws MalformedURLException {
    9.         URL wsdlURL = new URL("http://127.0.0.1:8080/jaxWsTest/services/WebServiceSample?wsdl");
    10.         QName SERVICE_NAME = new QName("http://test.ws.jax/""WebServiceSampleImplService");
    11.         Service service = Service.create(wsdlURL, SERVICE_NAME);
    12.         WebServiceSample client = service.getPort(WebServiceSample.class);
    13.         String result = client.say("kaven");    
    14.         System.out.println("Response from server : "+result);
    15.     }
    16. }
       4. Advanced Tech
            I. Configure the client invoking ConnectionTimeout and ReceiveTimeout
    1.         Client client = (Client) ClientProxy.getClient(port);
    2.         HTTPConduit http = (HTTPConduit) client.getConduit();
    3.         HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
    4.         httpClientPolicy.setConnectionTimeout(300);  // set ConnectionTimeout
    5.         httpClientPolicy.setAllowChunking(false);
    6.         httpClientPolicy.setReceiveTimeout(3200);  // set ReceiveTimeout
    7.         http.setClient(httpClientPolicy);
           more info refer to :http://cwiki.apache.org/CXF20DOC/client-http-transport-including-ssl-support.html
      
            II.  那么多的jar包,是不是很晕呢, 刚才花了几个小时,终于找到了Java客户端调用CXF Web-Service最小Jar包子集。
                          
           
                    图中所示的都是必要的,缺一不可,如果是standalone那么jboss下的两个必要的,如果在Jboss container中,可以不要          
                 III. Asynchronous WebService Calling    

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    IOS图像处理(6)在内存上下文中绘图
    IOS图像处理(5)填充模式
    IOS图像处理(4)坐标变化
    IOS图像处理(3)绘制路径
    IOS图像处理(2)绘制文本
    IOS图像处理(1)绘制简单的几何图形
    containsString
    UIScrollView zoom in/out center
    Mac Sublime Text 2 快捷键(转)
    Characteristics with cached values must be read-only
  • 原文地址:https://www.cnblogs.com/significantfrank/p/4875862.html
Copyright © 2011-2022 走看看