CXF 2.0 webservice 学习笔记系列

对比了一下CXF2 和 AXIS 2 还是选择在框架中集成CXF2。不过CXF2的文档更新的真是慢啊,试了一下,目前CXF2支持的两种绑定方式jaxb2和aegis,看了半天源码总算调通了。又忍不住批一下他的文档,给的都是半调子的例子,希望开发小组能尽快完善啊!

先放一个HelloWold的aegis的配置:
一、server 端:
1. HelloWorld.java
Java代码
package demo.spring;  
 
import javax.jws.WebService;  
 
@WebService 
public interface HelloWorld {  
    String sayHi(String text);  


package demo.spring;

import javax.jws.WebService;

@WebService
public interface HelloWorld {
    String sayHi(String text);
}


2.HelloWorldImpl.java
Java代码
package demo.spring;  
 
import javax.jws.WebService;  
 
@WebService(endpointInterface = "demo.spring.HelloWorld")  
public class HelloWorldImpl implements HelloWorld {  
 
    public String sayHi(String text) {  
        return "Hello " + text;  
    }  


package demo.spring;

import javax.jws.WebService;

@WebService(endpointInterface = "demo.spring.HelloWorld")
public class HelloWorldImpl implements HelloWorld {

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


3.beans.xml
Java代码
<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" />  
 
    <bean id="jaxWsServiceFactoryBean" 
        class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean">  
        <property name="wrapped" value="true" />  
        <property name="dataBinding" ref="aegisBean" />  
    </bean>  
 
    <bean id="aegisBean" 
        class="org.apache.cxf.aegis.databinding.AegisDatabinding" />  
 
    <jaxws:endpoint id="CollectiveServices" 
        implementor="demo.spring.HelloWorldImpl" address="/HelloWorld">  
        <jaxws:serviceFactory>  
            <ref bean="jaxWsServiceFactoryBean"/>  
        </jaxws:serviceFactory>  
    </jaxws:endpoint>  
</beans> 

<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" />

<bean id="jaxWsServiceFactoryBean"
class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean">
<property name="wrapped" value="true" />
<property name="dataBinding" ref="aegisBean" />
</bean>

<bean id="aegisBean"
class="org.apache.cxf.aegis.databinding.AegisDatabinding" />

<jaxws:endpoint id="CollectiveServices"
implementor="demo.spring.HelloWorldImpl" address="/HelloWorld">
<jaxws:serviceFactory>
<ref bean="jaxWsServiceFactoryBean"/>
</jaxws:serviceFactory>
</jaxws:endpoint>
</beans>


4.web.xml
Java代码
<web-app 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">  
 
    <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>  
        <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>/ws/*</url-pattern>  
    </servlet-mapping>  
 
</web-app> 

<web-app 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">

<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>
<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>/ws/*</url-pattern>
</servlet-mapping>

</web-app>


二、客户端

1.ClientTest.java
Java代码
package demo.spring.client;  
 
import static org.junit.Assert.assertEquals;  
 
import org.apache.cxf.aegis.databinding.AegisDatabinding;  
import org.apache.cxf.frontend.ClientProxyFactoryBean;  
import org.junit.AfterClass;  
import org.junit.BeforeClass;  
import org.junit.Test;  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
 
import demo.spring.HelloWorld;  
 
public final class ClientTest {  
 
    private static ClassPathXmlApplicationContext context;  
 
    @BeforeClass 
    public static void beforeClass() {  
        context = new ClassPathXmlApplicationContext(  
                new String[] { "demo/spring/client/client-beans.xml" });  
    }  
 
    @AfterClass 
    public static void afterClass() {  
        context = null;  
    }  
 
    @Test 
    public void testSayHiWithSpringConfig() throws Exception {  
 
        HelloWorld client = (HelloWorld) context.getBean("client");  
 
        String response = client.sayHi("Joe");  
        assertEquals("Hello Joe", response);  
    }  
      
    /** 
     * 这个官方有例子(http://cwiki.apache.org/CXF20DOC/aegis-databinding.html) 
     *  
     * @throws Exception 
     */ 
    @Test 
    public void testSayHiByCode() throws Exception {  
 
         ClientProxyFactoryBean factory = new ClientProxyFactoryBean();  
         factory.setServiceClass(HelloWorld.class);  
         factory.setAddress("http://localhost:8080/testCxf/ws/HelloWorld");  
         factory.getServiceFactory().setDataBinding(new AegisDatabinding());  
         HelloWorld client = (HelloWorld) factory.create();  
 
         assertEquals("Hello Joe", client.sayHi("Joe"));  
    }     


package demo.spring.client;

import static org.junit.Assert.assertEquals;

import org.apache.cxf.aegis.databinding.AegisDatabinding;
import org.apache.cxf.frontend.ClientProxyFactoryBean;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import demo.spring.HelloWorld;

public final class ClientTest {

private static ClassPathXmlApplicationContext context;

@BeforeClass
public static void beforeClass() {
context = new ClassPathXmlApplicationContext(
new String[] { "demo/spring/client/client-beans.xml" });
}

@AfterClass
public static void afterClass() {
context = null;
}

@Test
public void testSayHiWithSpringConfig() throws Exception {

HelloWorld client = (HelloWorld) context.getBean("client");

String response = client.sayHi("Joe");
assertEquals("Hello Joe", response);
}

/**
* 这个官方有例子(http://cwiki.apache.org/CXF20DOC/aegis-databinding.html)
*
* @throws Exception
*/
@Test
public void testSayHiByCode() throws Exception {

ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
factory.setServiceClass(HelloWorld.class);
factory.setAddress("http://localhost:8080/testCxf/ws/HelloWorld");
factory.getServiceFactory().setDataBinding(new AegisDatabinding());
HelloWorld client = (HelloWorld) factory.create();

assertEquals("Hello Joe", client.sayHi("Joe"));
}
}



2.client_beans.xml(testSayHiWithSpringConfig()中需要使用的配置文件)

Java代码
<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-2.0.xsd  
http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">  
 
      
    <bean id="aegisBean" 
        class="org.apache.cxf.aegis.databinding.AegisDatabinding" />  
          
    <bean id="serviceFactoryBean" class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean">  
        <property name="dataBinding" ref="aegisBean" />  
    </bean>  
 
    <bean id="client" class="demo.spring.HelloWorld" 
        factory-bean="clientFactory" factory-method="create" />  
 
    <bean id="clientFactory" 
        class="org.apache.cxf.frontend.ClientProxyFactoryBean">  
        <property name="serviceFactory" ref="serviceFactoryBean" />  
        <property name="serviceClass" 
            value="demo.spring.HelloWorld" />  
        <property name="address" 
            value="http://localhost:8080/testCxf/ws/HelloWorld" />  
    </bean>  
 
</beans> 


你可能感兴趣的:(java,apache,spring,webservice,JUnit)