接口类和实现类:

package com.demo;

import java.util.List;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface HelloWorld {
    String sayHi(@WebParam(name = "text") String text);// http://localhost:8080/simple_cxf_ws/webservice/helloWorld/sayHi/text/a

    String sayHiToUser(@WebParam(name = "user") Users user);

    String[] SayHiToUserList(@WebParam(name = "userList") List<Users> userList);
}
package com.demo;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.jws.WebService;

@WebService(endpointInterface = "com.demo.HelloWorld", serviceName = "HelloWorld")
public class HelloWorldImpl implements HelloWorld {

    Map<Integer, Users> users = new LinkedHashMap<Integer, Users>();

    public String sayHi(String text) {
        return "Hello " + text;
    }

    public String sayHiToUser(Users user) {
        users.put(users.size() + 1, user);
        return "Hello " + user.getName();
    }

    public String[] SayHiToUserList(List<Users> userList) {
        String[] result = new String[userList.size()];
        int i = 0;
        for (Users u : userList) {
            result[i] = "Hello " + u.getName();
            i++;
        }
        return result;
    }
}

接下来就是JAVABEAN,Users类:

package com.demo;

public class Users {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

我们要用做WEB项目吗?不急,先不用,CXF自带了一个轻量的容器服务,相当于spring自己提供了IOC容器一样。我们可以先用它来测试一下我们部署成功没。

直接来一个测试类来暴露 web服务(直接run,在浏览器访问http://localhost:8080/helloWorld?wsdl):

package com.demo;

import javax.xml.ws.Endpoint;

public class webServiceApp {
    public static void main(String[] args) {
        System.out.println("web service start");
        HelloWorldImpl implementor = new HelloWorldImpl();
        String address = "http://localhost:8080/helloWorld";
        Endpoint.publish(address, implementor);
        System.out.println("web service started");
    }
}

它生成了我们所需要的wsdl文件,说明我们部署成功了。

部署成功后,我们就是要调用啦,它的调用也相当简单,跟xfire类似,取得接口,然后就可以跟本地类一样调用方法了。

package com.demo;

import java.util.ArrayList;
import java.util.List;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloWorldClient {

    public static void main(String[] args) {

        // 创建WebService客户端代理工厂
        JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean();
        // 注册WebService接口
        svr.setServiceClass(HelloWorld.class);
        // 设置WebService地址
        svr.setAddress("http://localhost:8080/helloWorld");
        HelloWorld hw = (HelloWorld) svr.create();
        Users user = new Users();
        user.setName("Tony");
        System.out.println(hw.sayHiToUser(user));

        ApplicationContext context = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        HelloWorld client = (HelloWorld) context.getBean("client");
        Users user1 = new Users();
        user1.setName("Tony");
        Users user2 = new Users();
        user2.setName("freeman");
        List<Users> userList = new ArrayList<Users>();
        userList.add(user1);
        userList.add(user2);
        String[] res = client.SayHiToUserList(userList);
        System.out.println(res[0]);
        System.out.println(res[1]);
    }
}

这里很简单,也是取得一个工厂类,然后直接设接口和地址再create就可以得取相应的接口了,这里跟xfire一样,也是需要调用端先定义好接口原型,否则这些调用将无从说起。

但很多情况下,我们并不希望我们的webservice和我们的应用分开两个服务器,而希望他们在同一个容器,tomcat或JBOSS或其他的,这样我们就必须通过WEB来部署我们前面完成的webservice。

注意,我们这里需要用到spring定义文件。

首先看看web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>


    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/classes/applicationContext.xml</param-value>
    </context-param>

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>



    <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <display-name>CXFServlet</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>

这里很简单,只是指定了spring的监听器和相应的配置文件路径,并且指定了CXF的拦截方式。

接下来看看applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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.demo.HelloWorldImpl"
        address="/helloWorld" />

    <bean id="client" class="com.demo.HelloWorld" factory-bean="clientFactory"
        factory-method="create" />

    <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
        <property name="serviceClass" value="com.demo.HelloWorld" />
        <property name="address"
            value="http://localhost:8080/simple_cxf_ws/helloWorld" />
    </bean>


</beans>

 这里很简单,只是通过jaxws:endpoint定义了一个webservice,implementor是webservice的处理类,而address是它的访问路径,跟我们前面写的readerService类似。

这时我们可以把它部署到tomcat中,通过http://localhost:8080/simple_cxf_ws/helloWorld?wsdl  可以直接访问。

有些朋友会问,为什么这次访问的URL跟前面的不一样呢。其实前面的访问地址是我们自己定义的,而这里的webservice地址是我们在配置文件中配置 好的,并且是通过web项目来部署的,这里就需要用项目名称,而且我们在CXFServlet那里配置了url-pattern是/*, 所以最后的URL就跟上面一致了。

可以再次用前面的测试类测试一下,注意,需要把address修改成我们发布后的URL。

CXF相比xfire又更简洁了一些,虽然它增加了一些注解,但这些无伤大雅,它只是把以前的services.xml中的信息集中到类中,反而更 方便维护,但这还是见仁见智的,有些人就喜欢配置文件,而有些人就不喜欢。另外CXF的调用方式更加简洁,比起xfire它的代码量更小了,是一个较大的 进步。