CXF WebService服务端与测试

由于做项目需要写一个手机客户端访问之前做好的项目,所以需要了解Apache CXF服务框架,调试了很久才把服务端跟测试的弄好,写下来免得以后忘记了
首先建一个web service project,然后把CXF相关的jar放到lib目录下,所需最少的jar包有:
cxf-2.6.0.jar
geronimo-annotation_1.0_spec-1.1.1.jar
geronimo-jaxws_2.2_spec-1.1.jar
geronimo-stax-api_1.0_spec-1.0.1.jar
geronimo-ws-metadata_2.0_spec-1.1.3.jar
jaxb-api-2.2.6.jar
jaxb-impl-2.2.5.jar
neethi-3.0.2.jar
wsdl4j-1.6.2.jar
xmlSchema-core-2.0.2.jar
官网下载地址:http://cxf.apache.org/download.html

因为我用到了Spring,所以还要加Spring的jar包,jar包加好后修改web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">
	<display-name></display-name>
	
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
     	 classpath:applicationContext.xml
    	 classpath:cxf.xml
     </param-value>
	</context-param>

	<servlet>
		<servlet-name>CXFServlet</servlet-name>
		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
		<load-on-startup>0</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>CXFServlet</servlet-name>
		<url-pattern>/webServices/*</url-pattern>
	</servlet-mapping>

</web-app>

applicationContext.xml是Spring的配置文件没什么好说的,cxf.xml是配置CXF的,如下:

<?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="verificationService" implementor="VerificationServiceImpl" address="/verification" />
	    
        </beans>

注意import的3句要加进去,是CXF的配置文件,在cxf-2.6.0.jar里能找到,不过我把这三句注释掉也能运行,测试也能通过,真奇怪···最后一句的address属性跟等下的访问地址有关,等下再说明,配置文件弄好了之后就要写服务接口了,

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

@WebService
public interface VerificationService {
	public String verification(@WebParam(name="securityCode") String securityCode);

}

@WebhParam是接收客户端传过来的参数的,没有的话就不用写了,接口的实现类叫VerificationServiceImpl,在重写方法的时候要注意对null值的判断,不然会测试的时候会报错:Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: Fault occurred while processing.  这样服务端就写好了,启动tomcat,输入网址:
http://localhost:8080/VTCManagerService/webServices/verification?wsdl,其中VTCManagerService是项目名,webService是在web.xml中配置的,而verification就是上面写到的address属性,正常的话就能看到一些xml文件,其中有个targetNamespace的属性就是客户端要访问服务端所需要的,不过在测试的时候用
不到,下面是测试部分:
为了方便我是在服务端直接写个类做测试的,如果是新建一个工程的话可以把服务端的工程导出为jar文件放在新建工程lib目录下,测试类代码:

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import VerificationService;

public class TestWebService {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		JaxWsProxyFactoryBean jfb = new JaxWsProxyFactoryBean();
		jfb.setServiceClass(VerificationService.class);  
		jfb.setAddress

("http://localhost:8080/VTCManagerService/webServices/verification");  
		VerificationService vs = (VerificationService) jfb.create();  
		vs.verification("123456789");  
	}

}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值