CXFWebservice使用

来自ling
跳转至: 导航搜索

vat中的实现

  • deloitte.tms.vat.web 项目已经添加了webservice的支持,请参考下面的文档
  • was的实现暂时放在deloitte.tms.vat.web项目中,调用server层的代码来暴露接口,后期考虑移动到单独的web项目.
  • 参考CXFDemo CXFDemoImpl cxfdemo-beans.xml TestEndpoint

server流程

编写接口和接口实现

@WebService
public interface CXFDemo {
     public String sayHello(String foo);
}

@WebService()
public class CXFDemoImpl implements CXFDemo {

    public String sayHello(String foo) {
        return "hello "+foo;
    }

}

发布server

  • src\main\resources\beans\ws\xxx-beans.xml(xxx为接口名称小写) src\main\resources\beans\ws为放置文件规范
<?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:context="http://www.springframework.org/schema/context"  
xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
    http://www.springframework.org/schema/context   
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">   

    <jaxws:endpoint id="cxfDemo" implementor="com.deloitte.tms.vat.ws.sample.impl.CXFDemoImpl" address="/cxfdemo" />
</beans>

查看server接口

单元测试

  • 启动项目
  • 编写代码示例如下:
package com.deloitte.tms.vat.ws.sample;

import javax.xml.ws.Endpoint;

import junit.framework.TestCase;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.junit.Assert;

import com.deloitte.tms.vat.ws.sample.impl.CXFDemoImpl;

public class TestEndpoint extends TestCase {
    
    private static final String ADDRESS = "http://localhost:8080/vat/services/cxfdemo"; 
    protected void setUp() throws Exception {
        super.setUp();
        
        System.out.println("Starting Server");  
        CXFDemoImpl demo = new CXFDemoImpl();  
        
        Endpoint.publish(ADDRESS, demo);
        System.out.println("Start success");
    }
    
    public void testSayHello(){
        
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setServiceClass(CXFDemo.class);
        factory.setAddress(ADDRESS);
        CXFDemo client = (CXFDemo)factory.create();
        Assert.assertEquals(client.sayHello("foo"), "hello foo");
    }
}

基础设置(已配置好)

添加maven支持

		<dependency>  
		    <groupId>org.apache.cxf</groupId>  
		    <artifactId>cxf-rt-frontend-jaxws</artifactId>  
		    <version>2.7.7</version>  
		</dependency>  
		<dependency>  
		    <groupId>org.apache.cxf</groupId>  
		    <artifactId>cxf-rt-transports-http</artifactId>  
		    <version>2.7.7</version>  
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http-jetty</artifactId>
			<version>2.7.7</version>
		</dependency>

添加web.xml,并设置权限过滤

  • web.xml
    <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>
  • src/main/resources/beans/context/applicationContext-security.xml
	<beans:bean parent="ling2.anonymousUrl">
	  <beans:property name="urlPattern" value="/services/**"></beans:property> 
	</beans:bean>