Web服务(Web Services)是一种基于网络的标准化的软件系统,允许不同的应用程序通过网络相互通信和交互。它们使用标准化的网络协议和数据格式,使得不同平台、不同语言编写的应用程序能够互相通信和交换数据。
在现代软件开发中,构建可靠的Web服务是至关重要的。Apache CXF是一个功能强大的Java框架,用于构建Web服务和Web应用程序。结合Spring Boot,我们可以快速搭建一个简单的WebService。本文将介绍如何使用Apache CXF和Spring Boot创建一个简单的WebService,并进行基本的测试。
Web服务通常分为两种主要类型:
Web服务的架构通常包括以下关键组件:
Web服务具有以下主要特点:
Web服务在许多场景下都得到了广泛应用,包括但不限于:
一些常见的Web服务标准和技术包括:
首先,我们需要在项目中添加必要的依赖项。这些依赖项将帮助我们集成Apache CXF到Spring Boot应用程序中。我的使用的是gradle构建的项目
// 引入WebService
implementation 'org.apache.cxf:cxf-rt-frontend-jaxws:3.2.0'
implementation 'org.apache.cxf:cxf-rt-transports-http:3.2.0'
<dependency>
<groupId>org.apache.cxfgroupId>
<artifactId>cxf-rt-frontend-jaxwsartifactId>
<version>3.2.0version>
dependency>
<dependency>
<groupId>org.apache.cxfgroupId>
<artifactId>cxf-rt-transports-httpartifactId>
<version>3.2.0version>
dependency>
import com.fhr.student.entity.Student;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface StudentService {
/**
* 根据姓名获取学生信息
* @param userName 学生姓名
* @return 学生信息
*/
Student getStudentInfoByName(@WebParam(name = "userName")String userName);
}
import com.fhr.service.StudentService;
import com.fhr.student.entity.Student;
import org.springframework.stereotype.Component;
import javax.jws.WebService;
/**
* targetNamespace:目标命名控件,一般由接口所在包路径命名,不过是由里往外写:比如:我接口所在路径为:com.fhr.service 写为:http://service.fhr.com/
*/
@Component
@WebService(targetNamespace = "http://service.fhr.com/",endpointInterface = "com.fhr.service.StudentService")
public class StudentImpl implements StudentService {
/**
* 根据学生姓名获取学生信息
* @param userName 学生姓名
* @return 学生信息
*/
@Override
public Student getStudentInfoByName(String userName) {
// TODO这里应该查询数据库
System.out.println("传入的参数为:"+userName);
Student student = new Student();
student.setUserName(userName);
student.setClassName("高三一班");
student.setAge(14);
return student;
}
}
我们需要配置CXF和发布WebService的端点。我们使用Spring Boot的配置类来完成这个任务。
import com.fhr.service.StudentService;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;
@Configuration
public class WebServiceConfig {
// 创建一个SpringBus Bean,作为Apache CXF的默认总线
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
// 注册CXF Servlet,用于处理WebService请求
@Bean(name = "wbsBean")
public ServletRegistrationBean dispatcherServlet() {
// 创建一个ServletRegistrationBean,将CXFServlet映射到指定路径
ServletRegistrationBean wbsServlet = new ServletRegistrationBean(new CXFServlet(), "/wbs/*");
return wbsServlet;
}
// 定义WebService端点
@Bean
public Endpoint endpointPurchase(SpringBus springBus, StudentService studentService) {
// 创建EndpointImpl对象,并将SpringBus和WebService实现类传入
EndpointImpl endpoint = new EndpointImpl(springBus, studentService);
// 将端点发布到指定路径
endpoint.publish("/user-server");
// 打印发布成功消息,显示服务的访问地址
System.out.println("服务发布成功!地址为:http://localhost:8081/wbs/user-server");
// 返回端点对象
return endpoint;
}
}
启动项目后,您可以在浏览器中输入
http://localhost:8081/wbs/user-server?wsdl来查看WebService的WSDL文档。
# 启动项目
在浏览器的地址中输入 http://localhost:8081/wbs/user-server?wsdl

# 测试客户端
为了方便直接在本地项目测试
在浏览器中输入 测试
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/stu")
public class StudentController {
// 定义了一个映射到路径"/stu/getUserInfoByName"的GET请求处理方法
@GetMapping("/getUserInfoByName")
public Object[] getUserInfoByName(@RequestParam("name")String name){
// 创建JaxWsDynamicClientFactory实例,用于动态创建客户端
JaxWsDynamicClientFactory proxyFactoryBean = JaxWsDynamicClientFactory.newInstance();
// 使用动态客户端工厂创建客户端对象,并指定WebService的WSDL地址
Client client = proxyFactoryBean.createClient("http://localhost:8081/wbs/user-server?wsdl");
// 定义一个Object数组用于存储调用WebService方法后的返回结果
Object[] objects = new Object[0];
// 调用远程WebService方法
try {
// 调用客户端的invoke方法,传入方法名和参数,获取WebService方法的返回结果
objects = client.invoke("getStudentInfoByName", name);
} catch (Exception e) {
// 捕获异常,打印异常信息
e.printStackTrace();
}
// 返回WebService方法的返回结果
return objects;
}
}
