spring + cxf 构建 webservice 接口入门

简单的使用spring、cxf构建webservice

eclipse项目结构,和pom文件(使用maven构建的项目)

pom.xml需要依赖的包

<properties>
  	<cxf.version>2.4.3</cxf.version>
  	<jdk.version>1.6</jdk.version>
  </properties>
  
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version> 
      <scope>test</scope>
    </dependency>
    
    <!--CXF -->
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http</artifactId>
			<version>${cxf.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-management</artifactId>
			<version>${cxf.version}</version>
		</dependency>
		<dependency>
    		<groupId>org.apache.cxf</groupId>
    		<artifactId>cxf-rt-transports-http-jetty</artifactId>
    		<version>${cxf.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxws</artifactId>
			<version>${cxf.version}</version>
			<exclusions>
				<exclusion>
					<artifactId>asm</artifactId>
					<groupId>asm</groupId>
				</exclusion>
			</exclusions>
		</dependency>  
    <dependency>
			<groupId>javax.jws</groupId>
			<artifactId>jsr181-api</artifactId>
			<version>1.0-MR1</version>
		</dependency> 
  </dependencies>


首先定义webservice服务接口:CxfService

package com.test;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public interface CxfService {

	@WebMethod
	public String helloWorld(String name);
	
}
接口实现类CxfServiceImpl
package com.test;

import javax.jws.WebService;

@WebService()
public class CxfServiceImpl implements CxfService{

	public String helloWorld(String name) {
		// TODO Auto-generated method stub
		return "Hello World " + name;
	}

}

测试类TestDemo

package com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.test.spring.executor.TestService;
import com.test.spring.executor.TestTask;

public class TestDemo { 
	    public static void main(String[] args) {  
	    	 ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
//	    	 JaxWsProxyFactoryBean factory =  context.getBean(JaxWsProxyFactoryBean.class);  
	    	 
                 //获取客户端实例
	    	 CxfService client = (CxfService)context.getBean("cxfServiceClient");
	    	 
//	    	 CxfService client = (CxfService)factory.create();  
		        String respone= client.helloWorld("cyt");  
		        System.out.println(respone);   
	    }  
	   

}

spring相关配置applicationContext.xml(简单起见,服务和客户端放在了同一个项目,要在另一个项目中调用webservice可自行百度)

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

	<!-- cxf引用 -->
	<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="cxfService" class="com.test.CxfServiceImpl" />

	<!-- 发布服务  -->
	<jaxws:endpoint id="cxfServicePublic" implementor="#cxfService"
		address="/cxfService" />

	<!-- 调用服务客户端 -->
	<jaxws:client id="cxfServiceClient" serviceClass="com.test.CxfService"
		address="http://localhost:8080/WebServiceDemo/webservice/cxfService"></jaxws:client>

</beans>
web.xml配置cxf处理webservice服务请求的CXFServlet

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE web-app PUBLIC  
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"  
 "http://java.sun.com/dtd/web-app_2_3.dtd" >  
  
<web-app>  
  <display-name>Archetype Created Web Application</display-name>  
  <context-param>  
    <param-name>contextConfigLocation</param-name>  
    <param-value>classpath:applicationContext.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>  
  </servlet>  
  <servlet-mapping>  
    <servlet-name>CXFServlet</servlet-name>  
    <url-pattern>/webservice/*</url-pattern>  
  </servlet-mapping>  
  
  <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app> 
然后部署到Tomcat服务器

然后运行测试类TestDemo,结果如下:


服务调用成功。

要验证服务是否启动成功可以在浏览器中输入 : http://localhost:8080/WebServiceDemo/webservice/cxfService?wsdl

(格式:http:// + ip + 端口 + 项目名称 + 服务名称 + ?wsdl)

浏览器显示一组xml信息,说明服务启动成功。

初次接触webservice,做下学习笔记,有不妥的地方,还请指出或者有补充的可以下方留言,谢谢!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值