webService学习之cxf与spring的简单整合

本文详细介绍了如何使用CXF框架构建RESTful Web Service,包括导入依赖、定义接口、实现类、配置web.xml文件、创建ApplicationContext.xml并部署服务。

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

直接上代码吧:

一:先把cxf的jar包导进去,我是直接在官网下载的cxf的包,解压后直接把lib文件夹里的jar一次
性全部丢进去了,因为在学习,还不知道哪些包要哪些包不要的,所以干脆直接放进去

二:写一个简单的接口


package test.wervice;
import javax.jws.WebService;

@WebService
public interface HelloWorld {
void sayHi(String name);
}


三:写一个接口实现类
package test.wervice.impl;

import javax.jws.WebService;

import test.wervice.HelloWorld;

@WebService(endpointInterface="test.wervice.HelloWorld")
public class HelloWorldImpl implements HelloWorld{

public void sayHi(String name) {
System.out.println("helloWorld"+name);
}
}


四:配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 配置application的加载 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>
<!-- spring的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置cxf的servlet -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/cxf/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>


五:在WEB-INF下面创建applicationContext.xml,配置如下
<?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:p="http://www.springframework.org/schema/p"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<!-- 实现接口的bean -->
<bean id="hello" class="test.wervice.impl.HelloWorldImpl"/>

<!--第一种配置-->

<jaxws:endpoint address="/HelloWorld" >
<jaxws:implementor ref="hello" />
</jaxws:endpoint>

<!-- 第二种配置方法 -->
<!-- <jaxws:endpoint address="/HelloWorld" implementor="test.wervice.impl.HelloWorldImpl"/> -->

</beans>


最后启动项目,在浏览器端输入address的地址:http://localhost:8082/testWebservice/cxf/HelloWorld?wsdl

得到如下结果:


[img]http://dl2.iteye.com/upload/attachment/0103/6736/3b3c9a70-40cd-3df9-8ad6-fdcc1e6dae51.jpg[/img]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值