Spring集成CXF

本文介绍如何搭建WebService服务端与客户端,包括定义接口与实现类、配置Spring XML文件及在web.xml中进行设置。同时提供了测试连接成功的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

直接代码

服务器端

定义接口和实现类

IHelloWorld.java

package com.sf.module.lscmcommon.webservice;

import java.util.List;
import javax.jws.WebService;

@WebService
public interface IHelloWorld {

	public String sayHello(String text);

	public List<String> findObj();

}

HelloWorld.java

package com.sf.module.lscmcommon.webservice;

import java.util.List;

import javax.jws.WebService;

@WebService
public class HelloWorld implements IHelloWorld {

	public String sayHello(String text) {
		System.out.println("Hello" + text);
		return "Hello" + text;
	}

	public List<String> findObj(){
		return null;
	}
}

配置Spring的xml文件

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd     
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd     
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://cxf.apache.org/jaxws 
http://cxf.apache.org/schemas/jaxws.xsd"

xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jaxws="http://cxf.apache.org/jaxws">
    <!-- 这三个必加 -->
    <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" /> 
    
	
	<!-- cxf webService 接口实现类 -->
	<bean id="hello" class="com.sf.module.lscmcommon.webservice.HelloWorld" /> 
	<!-- 
		id:指在spring配置的bean的ID.
		Implementor:指明具体的实现类.
		Address:指明这个web service的相对地址,如:http://10.0.7.115:8080/lscm/ws/IHelloWorld?wsdl
	-->
    <jaxws:endpoint id="helloWorld" implementor="#hello" 
        address="/IHelloWorld" /> 
        
	
</beans>

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

服务端完成,部署tomcat,打开地址.出现这玩意就是成功了.



客户端开发

新建一个工程,定义一个接口即可,和服务端的一致.

IHelloWorld .java

package com.cdg.webService;

import javax.jws.WebService;

@WebService
public interface IHelloWorld {
	public String sayHello(String text);  
}


定义Spring 的.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-2.0.xsd     
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-2.0.xsd     
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
		http://cxf.apache.org/jaxws 
		http://cxf.apache.org/schemas/jaxws.xsd"
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jaxws="http://cxf.apache.org/jaxws">

	<!-- 
		id:不需要我解释
		serviceClass:接口类包名
		address:服务器的地址,后面的?wsdl不要
	 -->
	<jaxws:client id="helloWorld"
		serviceClass="com.cdg.webService.IHelloWorld"
		address="http://10.0.7.115:8080/lscm/ws/IHelloWorld" />

	<!-- 加载数据源 -->
	<import resource="db-config.xml" />
</beans>

其他的都不用了;

编写个测试类看看有没有连接成功

package com.cdg.webService;

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

public class HelloWorld {

	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"classpath:springConfig/applicationContext.xml");
		IHelloWorld client = (IHelloWorld) ctx.getBean("helloWorld");
		String result = client.sayHello("你好!");
		System.out.println("回调 : " + result);
	}
}

以上的包自己去下载.

温馨提示:如果有报JAXB的包的错误,就看是不是和jdk版本有冲突,换一下jaxb的版本即可.

看效果===================================================================





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值