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    

时间: 2024-09-01 17:39:28

Head First WebService with CXF的相关文章

配置webservice的CXF框架不成功?

问题描述 配置webservice的CXF框架不成功? 这个配置一直跟着教程走的,教程里就没做其他操作了 解决方案 输入echo %path%,看下是什么,你的path怎么只有这一个了,别的去哪了.输入d:回车输入d:opensourceapache-cxf-2.6.2binwsdl2java 试试看

cxf加载问题-webservice 配置cxf总是报错

问题描述 webservice 配置cxf总是报错 ERROR 2015-10-21 11:16:39,846 [main] org.springframework.web.servlet.DispatcherServlet: Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'wobroadbandser': I

Spring mvc实现WEBSERVICE 和 CXF实现WEBSERVICE 各有什么特点

问题描述 其实我一直没弄明白, 实现webservice本质上就是一个请求,响应一串XML或者JSON或者其他什么格式类型,只有client和server定义好格式规则(协议),那么就可以通信,完成各种服务调用.springmvc 实现webservice比较直接易懂, 而且我觉得用起来也很好用.然后最近学习到CXF,我就一直没弄明白,为什么要用这个框架实现webservice? 它有什么优点? 解决方案 主要是webservice的协议:http+xml=soap既然是http协议,你只要按照

cxf webservice-关于动态调用webservice

问题描述 关于动态调用webservice 使用CXF动态客户端调用webservice,代码如下: JaxWsDynamicClientFactory dynamicClient = JaxWsDynamicClientFactory .newInstance(); String wsdl = "http://www.webxml.com.cn/WebServices/TranslatorWebService.asmx?wsdl"; Client client = dynamicCl

java cxf webservice整合问题

问题描述 java cxf webservice整合问题 jws代码: @Service("iom4crm") @WebService(targetNamespace = "http://service.iom.test.com/", serviceName="IOM4CRM") public class Iom4Crm implements IIom4Crm{ @Override @WebMethod(operationName = "

客户端使用何种方式调用webservice服务?

问题描述 知道了wsdl地址,因为参数都是基本类型,所以使用javax.xml.soap 包下的api自己发送SOAP消息,解析响应数据 .但速度是相当的慢!!如果使用xfire或axis2等webservice框架效率会不会有所提升?有没有不是基于XML实现的.效率比较高的webservice调用方式?各位前辈提提意见,小子先行谢过 问题补充:elicer 写道 解决方案 引用谢谢你的回答,有种恍然大悟的感觉!不过因为对Hessian 不熟,找了点资料说:服务器端必须具备以下几点: ·包含He

cxf-CXF+Spring做webservice 部署在was服务器上报错

问题描述 CXF+Spring做webservice 部署在was服务器上报错 Error 500: javax.servlet.ServletException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'handService': Invocation of init method failed; nested exception is javax.xml.w

那位高手有WebService能跑起来的例子

问题描述 那位高手有WebService能跑起来的例子 刚刚接触这个 那位仁兄 有能跑起来的 代码 给我参考下 谢谢啦 解决方案 请看看这篇博文,有什么不明白的可以问我.http://jadethao.iteye.com/blog/1706963希望你能自己动手完成.毕竟自己弄出来能学到很多东西,还能有成就感.若正想要现成的,我这有!解决方案二:一分钟速成webservice,http://java-frog.iteye.com/admin/blogs/808412解决方案三:现在人们提到web

WebService入门_01

1.XML约束-schema [html] view plain copy  print? <?xml version="1.0" encoding="UTF-8" ?>    <schema  xmlns="http://www.w3.org/2001/XMLSchema"        targetNamespace="http://www.wiseweb.com"       elementFormDe