qidao123.com技术社区-IT企服评测·应用市场

标题: SpringBoot 整合WebService [打印本页]

作者: 宁睿    时间: 2024-12-26 01:31
标题: SpringBoot 整合WebService
WebService

   Web服务(Web Services)是一种基于网络的尺度化的软件系统,答应差别的应用程序通过网络相互通讯和交互。它们使用尺度化的网络协媾和数据格式,使得差别平台、差别语言编写的应用程序能够相互通讯和互换数据。
  在现代软件开辟中,构建可靠的Web服务是至关重要的。Apache CXF是一个功能强大的Java框架,用于构建Web服务和Web应用程序。结合Spring Boot,我们可以快速搭建一个简单的WebService。本文将先容怎样使用Apache CXF和Spring Boot创建一个简单的WebService,并进行基本的测试。
  1.简单先容WebService

1.1. 范例

Web服务通常分为两种重要范例:

1.2. 架构

Web服务的架构通常包括以下关键组件:

1.3. 重要特点

Web服务具有以下重要特点:

1.4. 使用场景

Web服务在很多场景下都得到了广泛应用,包括但不限于:

1.5. Web服务尺度和技术

一些常见的Web服务尺度和技术包括:

2.案例-WebServiceDemo

2.1.引入配置文件

   首先,我们需要在项目中添加必要的依赖项。这些依赖项将资助我们集成Apache CXF到Spring Boot应用程序中。我的使用的是gradle构建的项目
  1.     // 引入WebService
  2.     implementation 'org.apache.cxf:cxf-rt-frontend-jaxws:3.2.0'
  3.     implementation 'org.apache.cxf:cxf-rt-transports-http:3.2.0'
复制代码
  1. <dependency>
  2.     <groupId>org.apache.cxf</groupId>
  3.     <artifactId>cxf-rt-frontend-jaxws</artifactId>
  4.     <version>3.2.0</version>
  5. </dependency>
  6. <dependency>
  7.     <groupId>org.apache.cxf</groupId>
  8.     <artifactId>cxf-rt-transports-http</artifactId>
  9.     <version>3.2.0</version>
  10. </dependency>
复制代码
2.2.创建接口

  1. import com.fhr.student.entity.Student;
  2. import javax.jws.WebParam;
  3. import javax.jws.WebService;
  4. @WebService
  5. public interface StudentService {
  6.     /**
  7.      * 根据姓名获取学生信息
  8.      * @param userName 学生姓名
  9.      * @return 学生信息
  10.      */
  11.     Student getStudentInfoByName(@WebParam(name = "userName")String userName);
  12. }
复制代码
2.3.创建接口实现类

  1. import com.fhr.service.StudentService;
  2. import com.fhr.student.entity.Student;
  3. import org.springframework.stereotype.Component;
  4. import javax.jws.WebService;
  5. /**
  6. * targetNamespace:目标命名控件,一般由接口所在包路径命名,不过是由里往外写:比如:我接口所在路径为:com.fhr.service 写为:http://service.fhr.com/
  7. */
  8. @Component
  9. @WebService(targetNamespace = "http://service.fhr.com/",endpointInterface = "com.fhr.service.StudentService")
  10. public class StudentImpl implements StudentService {
  11.     /**
  12.      * 根据学生姓名获取学生信息
  13.      * @param userName 学生姓名
  14.      * @return 学生信息
  15.      */
  16.     @Override
  17.     public Student getStudentInfoByName(String userName) {
  18.         // TODO这里应该查询数据库
  19.         System.out.println("传入的参数为:"+userName);
  20.         Student student = new Student();
  21.         student.setUserName(userName);
  22.         student.setClassName("高三一班");
  23.         student.setAge(14);
  24.         return student;
  25.     }
  26. }
复制代码
2.4.创建WebService配置类

   我们需要配置CXF和发布WebService的端点。我们使用Spring Boot的配置类来完成这个任务。
  1. import com.fhr.service.StudentService;
  2. import org.apache.cxf.Bus;
  3. import org.apache.cxf.bus.spring.SpringBus;
  4. import org.apache.cxf.jaxws.EndpointImpl;
  5. import org.apache.cxf.transport.servlet.CXFServlet;
  6. import org.springframework.boot.web.servlet.ServletRegistrationBean;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9. import javax.xml.ws.Endpoint;
  10. @Configuration
  11. public class WebServiceConfig {
  12.    
  13.     // 创建一个SpringBus Bean,作为Apache CXF的默认总线
  14.     @Bean(name = Bus.DEFAULT_BUS_ID)
  15.     public SpringBus springBus() {
  16.         return new SpringBus();
  17.     }
  18.     // 注册CXF Servlet,用于处理WebService请求
  19.     @Bean(name = "wbsBean")
  20.     public ServletRegistrationBean dispatcherServlet() {
  21.         // 创建一个ServletRegistrationBean,将CXFServlet映射到指定路径
  22.         ServletRegistrationBean wbsServlet = new ServletRegistrationBean(new CXFServlet(), "/wbs/*");
  23.         return wbsServlet;
  24.     }
  25.     // 定义WebService端点
  26.     @Bean
  27.     public Endpoint endpointPurchase(SpringBus springBus, StudentService studentService) {
  28.         // 创建EndpointImpl对象,并将SpringBus和WebService实现类传入
  29.         EndpointImpl endpoint = new EndpointImpl(springBus, studentService);
  30.         // 将端点发布到指定路径
  31.         endpoint.publish("/user-server");
  32.         // 打印发布成功消息,显示服务的访问地址
  33.         System.out.println("服务发布成功!地址为:http://localhost:8081/wbs/user-server");
  34.         // 返回端点对象
  35.         return endpoint;
  36.     }
  37. }
复制代码
2.5.测试

   启动项目后,您可以在欣赏器中输入http://localhost:8081/wbs/user-server?wsdl来查看WebService的WSDL文档。
  1. # 启动项目
  2.         在浏览器的地址中输入 http://localhost:8081/wbs/user-server?wsdl
复制代码

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


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 qidao123.com技术社区-IT企服评测·应用市场 (https://dis.qidao123.com/) Powered by Discuz! X3.4