wordpress 书 pdf,网站制作优化济南,北大青鸟网站建设,网站建设需要哪些网络技术文章目录 WebService1.简单介绍WebService1.1. 类型1.2. 架构1.3. 主要特点1.4. 使用场景1.5. Web服务标准和技术 2.案例-WebServiceDemo2.1.引入配置文件2.2.创建接口2.3.创建接口实现类2.4.创建WebService配置类2.5.测试 WebService Web服务#xff08;Web Services#xf… 文章目录 WebService1.简单介绍WebService1.1. 类型1.2. 架构1.3. 主要特点1.4. 使用场景1.5. Web服务标准和技术 2.案例-WebServiceDemo2.1.引入配置文件2.2.创建接口2.3.创建接口实现类2.4.创建WebService配置类2.5.测试 WebService Web服务Web Services是一种基于网络的标准化的软件系统允许不同的应用程序通过网络相互通信和交互。它们使用标准化的网络协议和数据格式使得不同平台、不同语言编写的应用程序能够互相通信和交换数据。 在现代软件开发中构建可靠的Web服务是至关重要的。Apache CXF是一个功能强大的Java框架用于构建Web服务和Web应用程序。结合Spring Boot我们可以快速搭建一个简单的WebService。本文将介绍如何使用Apache CXF和Spring Boot创建一个简单的WebService并进行基本的测试。 1.简单介绍WebService
1.1. 类型
Web服务通常分为两种主要类型
SOAP Web服务基于SOAPSimple Object Access Protocol协议的Web服务。SOAP是一种用于交换结构化信息的协议它使用XML作为消息格式并通常通过HTTP协议进行传输。RESTful Web服务基于RESTRepresentational State Transfer原则的Web服务。RESTful服务使用标准的HTTP方法如GET、POST、PUT、DELETE来执行操作并通常返回JSON或XML格式的数据。
1.2. 架构
Web服务的架构通常包括以下关键组件
服务提供者Service Provider提供Web服务的实体。它们发布服务并处理来自客户端的请求。服务请求者Service Requestor使用Web服务的客户端应用程序。它们向服务提供者发送请求并处理响应。服务描述Service DescriptionWeb服务的描述文件通常使用WSDLWeb Services Description Language或OpenAPI等格式来描述服务的接口和操作。消息格式Message FormatWeb服务使用的数据交换格式通常是XML或JSON。通信协议Communication ProtocolWeb服务之间通信的协议常见的包括HTTP、HTTPS、SMTP等。
1.3. 主要特点
Web服务具有以下主要特点
跨平台性Platform Independence由于Web服务使用标准化的协议和数据格式因此它们可以在不同的平台和操作系统上运行。松耦合Loose CouplingWeb服务通过标准化接口进行通信服务提供者和请求者之间的耦合度较低可以独立开发和部署。可组合性Composability可以通过组合多个Web服务来创建复杂的应用程序。可重用性ReusabilityWeb服务可以被多个应用程序重复使用从而提高了软件开发效率。易于维护Maintainability由于Web服务使用标准化的接口和协议因此易于维护和更新。
1.4. 使用场景
Web服务在许多场景下都得到了广泛应用包括但不限于
企业应用集成Enterprise Application IntegrationEAI将不同的企业应用程序和系统集成在一起实现数据和业务流程的无缝交互。分布式系统构建分布式系统和服务导向架构Service-Oriented ArchitectureSOA提供跨网络的服务和资源共享。移动应用程序开发通过Web服务为移动应用程序提供数据和功能支持与后端服务器进行通信和交互。云计算在云平台上部署和管理Web服务提供云端服务和资源。
1.5. Web服务标准和技术
一些常见的Web服务标准和技术包括
SOAPSimple Object Access Protocol用于构建基于XML的Web服务的协议。WSDLWeb Services Description Language用于描述Web服务的接口和操作的XML格式的语言。UDDIUniversal Description, Discovery, and Integration用于注册和发现Web服务的协议和规范。RESTRepresentational State Transfer一种基于HTTP协议的软件架构风格用于构建RESTful Web服务。JSONJavaScript Object Notation一种轻量级的数据交换格式通常用于RESTful Web服务的数据格式。
2.案例-WebServiceDemo
2.1.引入配置文件 首先我们需要在项目中添加必要的依赖项。这些依赖项将帮助我们集成Apache CXF到Spring Boot应用程序中。我的使用的是gradle构建的项目 // 引入WebServiceimplementation org.apache.cxf:cxf-rt-frontend-jaxws:3.2.0implementation org.apache.cxf:cxf-rt-transports-http:3.2.0dependencygroupIdorg.apache.cxf/groupIdartifactIdcxf-rt-frontend-jaxws/artifactIdversion3.2.0/version
/dependency
dependencygroupIdorg.apache.cxf/groupIdartifactIdcxf-rt-transports-http/artifactIdversion3.2.0/version
/dependency
2.2.创建接口
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);
}
2.3.创建接口实现类
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 学生信息*/Overridepublic Student getStudentInfoByName(String userName) {// TODO这里应该查询数据库System.out.println(传入的参数为userName);Student student new Student();student.setUserName(userName);student.setClassName(高三一班);student.setAge(14);return student;}
}
2.4.创建WebService配置类 我们需要配置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端点Beanpublic 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;}
}
2.5.测试 启动项目后您可以在浏览器中输入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;}
}