有没有哪位大侠给我写个cxf下的webservice通信的例子?我在网上下了好多,也试了好多,但是好像都不能用,写完之后全是错误,运行都运行不起来。不知道是写的不对还是包导的不对,希望大家帮忙,谢谢!

解决方案 »

  1.   

    spring + cxf 可以不?
      

  2.   

    Web Servics  是这个axis把  可以离线通讯的
      

  3.   

    例子来了
    下载jar包
    http://cxf.apache.org
    1.接口
     package com.cxf.service;import javax.jws.WebService;@WebService
    public interface HelloWord
    {
     String sayHi(String text);}
    2.实现类
          package com.cxf.service;import javax.jws.WebService;@WebService(endpointInterface = "com.cxf.service.HelloWord")
    public class HelloWordImpl implements HelloWord
    { public String sayHi(String text){
            return "Hi" + text;
        }
    }
    3.web.xml配置
        <?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE web-app
        PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd">
    <web-app>
       <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/beans.xml</param-value>
    </context-param> <listener>
    <listener-class>
    org.springframework.web.context.ContextLoaderListener
    </listener-class>
    </listener> <servlet>
    <servlet-name>CXFServlet</servlet-name>
    <display-name>CXF Servlet</display-name>
    <servlet-class>
    org.apache.cxf.transport.servlet.CXFServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet> <servlet-mapping>
    <servlet-name>CXFServlet</servlet-name>
    <url-pattern>/*</url-pattern>
    </servlet-mapping>
       
    </web-app>
    4.服务发布,需先写一个beans.xml(这里配置的是服务)
        <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
     
    <jaxws:endpoint 
      id="helloWorld" 
      implementor="com.cxf.service.HelloWordImpl" 
      address="http://localhost:8080/HelloWorld"/></beans>
    5.如果通过spring结合方式访问还需要配置一个客户端文件(applications.xml)放在src下面
      <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
     

        <jaxws:client id="helloClient"
                      serviceClass="com.cxf.service.HelloWord"
                      address="http://localhost:8080/HelloWorld" />
      
    </beans>
    6.发布
    启动tomact,在浏览器输入http://localhost:8080/HelloWorld?wsdl
    看是否能看到wsdl文件
    7.测试public static void main(String[] args)
    {
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); // your Spring ApplicationContext
    HelloWord client = (HelloWord) context.getBean("helloClient");
    String response = client.sayHi("jianglh");
    System.out.println(response); }
    通过cxf 提供的客户端代理工厂类来访问 public static void main(String[] args)
    {
    JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
    factory.setServiceClass(HelloWord.class);
    factory.setAddress("http://localhost:8080/HelloWorld");
    HelloWord iHelloWorld = (HelloWord) factory.create();
    System.out.println("invoke webservice...");
    System.out.println("message context is:" + iHelloWorld.sayHi("Josen"));
    //System.exit(0); 
    }
    通过另外一种方式访问
    private static final QName SERVICE_NAME = new QName("http://service.cxf.com/","HelloWord");
    private static final QName PORT_NAME = new QName("http://service.cxf.com/","HelloWordPort");


    public static void main(String[] args)
    {

    Service service =Service.create(SERVICE_NAME);
    String endpointAddress="http://localhost:8080/HelloWorld";
    service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);

    HelloWord  hi=service.getPort(HelloWord.class);
    String returnvalue = hi.sayHi("hello");

    System.out.println(returnvalue);


      

  4.   

    我有个可以在tomcat中跑的demo,给个邮箱发给你
      

  5.   

    好,[email protected].谢谢!
      

  6.   

    CXF想找个不是Spring的例子还不容易呢。
      

  7.   

    基于webservice1.4的示例已经发至你的邮箱,
    包含三个文件:
    A.搭载的axis1.4包
    B.示例源码
    C.说明.txt说明的内容:
    1.示例源码,是写的一个webservice示例的源程序
    2.搭载的axis1.4包是指示例源码部署到axis1.4服务上
    然后将axis1.4放到tomcat的webapps文件夹下,即可以运行这个webservice附件中的axis1.4文件夹可以直接放至tomcat的webapps下,即可运行。
    webservice的wsdl地址是http://localhost:8000/axis1_4/services/FirstWebService?wsdl
    IP和端口根据你自己机器而定
    3.WebserviceExample工程里的com.techown.axis.test包下的TestMain.java类是一个对webservice调用代码样例
    部署方式有点麻烦,使用步骤:
    1.在示例源码中的HelloWebService.java类的方法里写你的webservice服务器端的
    的逻辑代码
    2.逻辑代码完成后,将WebserviceExample工程,编译成class文件
    3.将src下的com文件夹,即编译好的所有class文件,拷贝到axis1_4\WEB-INF\classes下
    将原来的com文件夹覆盖(或将原来的com删除掉,用新的com文件夹来代替)
    4.将WebserviceExample工程下的\WebRoot\WEB-INF\web.xml文件复制,拷贝到axis1_4\WEB-INF文件夹
    替换web.xml文件
    5.将WebserviceExample工程下的\WebRoot\WEB-INF\server-config.wsdd文件复制,拷贝到axis1_4\WEB-INF文件夹
    替换server-config.wsdd文件
    6.将WebRoot\WEB-INF\lib文件夹下的jar包复制至axis1_4\WEB-INF\lib文件夹下
    7.完成一个基于axis1.4的webservice部署
      

  8.   


    麻烦给我也发一份 [email protected]
      

  9.   

    可以也给我发一个吗?[email protected]
    谢谢