cxf3.x +spring 3.x(4.x)+ maven 发布webservice 服务
cxf 在做企业级webservices 服务的时候确实非常好用,个人觉得比axis1, 2都好用。
虽然spring自身也提供了webservices发布方法,这里使用cxf跟spring结合,使用起来非常方便;
整体项目结果如下:
基于maven 来配置使得项目更加简单
-
新建一个maven 项目,补全 src下的main和test目录
-
打开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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>cxf</display-name>
<servlet>
<description>m CXF Endpoint</description>
<display-name>cxf</display-name>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>60</session-timeout>
</session-config>
</web-app>
- 新建一个cxf-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- START SNIPPET: beans -->
<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-servlet.xml"/>
<jaxws:endpoint id="DepartmentService" implementor="com.qycloud.oatos.ty.department.DepartmentServiceImpl" address="/DepartmentService"/>
<jaxws:endpoint id="UserService" implementor="com.qycloud.oatos.ty.user.UserServiceImpl" address="/UserService"/>
</beans>
- 补全 pom.xml ,主要是spring 的依赖和cxf,cxf 只需要以下2个
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.0.0</version>
</dependency>
- 编写代码:这里我们回头看到cxf-servlet.xml 里面可发现我们只需要制定一个id,一个是实现类,一个访问地址即可。
a) 编写一个接口
@WebService
public interface DepartmentService {
boolean updateTheOrg(String org);
boolean updateOrgsCode(String org);
}
b) 编写一个实现类
@WebService(endpointInterface = "com.qycloud.oatos.ty.department.DepartmentService")
public class DepartmentServiceImpl implements DepartmentService {
@Override
public boolean updateTheOrg(String org) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean updateOrgsCode(String org) {
// TODO Auto-generated method stub
return false;
}
}
7.剩下的事情就是发布了。右键run on server。结果如下
- demo下载地址
自吹自擂的互联网
分类:
JAVA
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?