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代码
    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">  
 
      
      
      
 
            class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean">  
          
          
   
  
 
            class="org.apache.cxf.aegis.databinding.AegisDatabinding" />  
 
            implementor="demo.spring.HelloWorldImpl" address="/HelloWorld">  
          
              
       
  
   
  
 

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





class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean">




class="org.apache.cxf.aegis.databinding.AegisDatabinding" />

implementor="demo.spring.HelloWorldImpl" address="/HelloWorld">







4.web.xml
Java代码
    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">  
 
      
        contextConfigLocation  
        WEB-INF/beans.xml  
   
  
 
      
          
            org.springframework.web.context.ContextLoaderListener  
       
  
   
  
 
      
        CXFServlet  
          
            org.apache.cxf.transport.servlet.CXFServlet  
       
  
        1  
   
  
 
      
        CXFServlet  
        /ws/*  
   
  
 
 

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


contextConfigLocation
WEB-INF/beans.xml




org.springframework.web.context.ContextLoaderListener




CXFServlet

org.apache.cxf.transport.servlet.CXFServlet

1



CXFServlet
/ws/*





二、客户端

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代码
    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">  
 
      
            class="org.apache.cxf.aegis.databinding.AegisDatabinding" />  
          
      
          
   
  
 
            factory-bean="clientFactory" factory-method="create" />  
 
            class="org.apache.cxf.frontend.ClientProxyFactoryBean">  
          
                    value="demo.spring.HelloWorld" />  
                    value="http://localhost:8080/testCxf/ws/HelloWorld" />  
   
  
 
 


你可能感兴趣的:(WebService,Spring,junit,Java,Apache)