SpringBoot WebService服务端&客户端使用教程

SpringBoot 集成 Webservice

🚩webservice是什么

Web服务是一种基于网络的软件系统,它通过标准化的协议(如HTTP、XML、SOAP、REST等)来实现不同计算机系统之间的交互和通信。Web服务允许不同的应用程序在网络上进行通信,以便能够相互访问和交换数据或功能。

Web服务通常遵循以下几个主要特征:

  1. 基于标准化协议:Web服务使用通用的标准化协议进行通信,其中最常见的是HTTP(用于传输)和XML(用于数据描述)。
  2. 面向开放性:Web服务是面向开放性的,可以由任何支持相应协议的应用程序进行访问和使用。
  3. 与平台无关:Web服务是与平台无关的,因为它们基于标准化协议和格式进行通信,所以不受特定平台或编程语言的限制。
  4. 松耦合性:Web服务通过提供标准化接口来实现与其他系统的交互,从而实现了松耦合的系统集成。

Web服务通常分为两种主要类型:

  • SOAP-based Web Services:基于SOAP(Simple Object Access Protocol)协议的Web服务,使用XML格式作为消息交换的载体,通常采用WSDL(Web Services Description Language)来描述服务接口。
  • RESTful Web Services:基于REST(Representational State Transfer)架构风格的Web服务,使用HTTP协议进行通信,通常使用JSON或XML格式进行数据交换。

Web服务的典型应用包括跨平台的数据交换、远程方法调用、系统集成、以及构建分布式应用程序等。它们在现代的分布式系统中起着至关重要的作用,为不同系统之间的通信提供了便捷和标准化的解决方案。

✔服务端:

1.依赖

xml 复制代码
<!--        webservice相关依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>


        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.2.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.2.0</version>
        </dependency>

2.服务接口

less 复制代码
package com.example.demo.service;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

/**
 * @author: Yinlei
 * Package: com.example.demo.service
 * @date: 2023-10-18 8:40
 * @Description: webservice测试
 * @version: 1.0
 */
@WebService(name = "HelloWebService",
targetNamespace = "http://helloWebService.service.demo.example.com")
public interface HelloWebService {
    @WebMethod
    @WebResult(name = "resultName")
    String get(@WebParam(name = "name") String name);
}

3.服务接口实现类

typescript 复制代码
package com.example.demo.service.Impl;

import com.example.demo.service.HelloWebService;
import org.springframework.stereotype.Service;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

/**
 * @author: Yinlei
 * Package: com.example.demo.service.Impl
 * @date: 2023-10-18 8:46
 * @Description:
 * @version: 1.0
 */
@Service
@WebService(name = "HelloWebService",
        targetNamespace = "http://helloWebService.service.demo.example.com", //命名空间,一般是对应的路径反过来
        endpointInterface = "com.example.demo.service.HelloWebService")  //实现接口的地址
public class HelloWebServiceImpl implements HelloWebService {
    @Override
    public String get( String name) {
        return name;
    }
}

4.cxf配置类

java 复制代码
package com.example.demo.config;

import com.example.demo.service.HelloWebService;
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.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

/**
 * @author: Yinlei
 * Package: com.example.demo.config
 * @date: 2023-10-18 10:09
 * @Description: Cxf配置类
 * @version: 1.0
 */
@Configuration
public class CxfConfig {
    @Autowired
    private HelloWebService helloWebService;

    @Bean
    public ServletRegistrationBean disServlet(){
        return new ServletRegistrationBean(new CXFServlet(),"/webService/*"); //webservice下的请求将有CXFServlet处理
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus(){
        return  new SpringBus();
    }

    @Bean
    public Endpoint endpoint(){
        EndpointImpl endpoint = new EndpointImpl(springBus(), helloWebService);
        endpoint.publish("/helloWebservice");
        return endpoint;
    }

}

✔客户端

java 复制代码
@GetMapping("/get1")
public String get1() throws MalformedURLException {
    //创建WSDL文件的URL,这个参数为暴露webervice的网址
    URL wsdlLocation = new URL("http://localhost:9000/webService/helloWebservice?wsdl");
    //创建服务名称:第一个参数为命名空间地址,第二个参数 为本地部分的命名 HelloWebServiceImplService  这个是对应的实现类名加上Service
    QName serviceName = new QName("http://helloWebService.service.demo.example.com", "HelloWebServiceImplService");
    Service service = Service.create(wsdlLocation, serviceName);

    //获取服务实现类  参数为对应的服务接口.class
    HelloWebService port = service.getPort(HelloWebService.class);
    //调用方法
    String asd = port.get("asd");
    return asd;
}

🍚总结

大功告成,撒花致谢🎆🎇🌟,关注我不迷路,带你起飞带你富。

相关推荐
程序员岳焱42 分钟前
Java 与 MySQL 性能优化:Java 实现百万数据分批次插入的最佳实践
后端·mysql·性能优化
麦兜*1 小时前
Spring Boot启动优化7板斧(延迟初始化、组件扫描精准打击、JVM参数调优):砍掉70%启动时间的魔鬼实践
java·jvm·spring boot·后端·spring·spring cloud·系统架构
大只鹅2 小时前
解决 Spring Boot 对 Elasticsearch 字段没有小驼峰映射的问题
spring boot·后端·elasticsearch
ai小鬼头2 小时前
AIStarter如何快速部署Stable Diffusion?**新手也能轻松上手的AI绘图
前端·后端·github
IT_10242 小时前
Spring Boot项目开发实战销售管理系统——数据库设计!
java·开发语言·数据库·spring boot·后端·oracle
bobz9652 小时前
动态规划
后端
stark张宇3 小时前
VMware 虚拟机装 Linux Centos 7.9 保姆级教程(附资源包)
linux·后端
亚力山大抵4 小时前
实验六-使用PyMySQL数据存储的Flask登录系统-实验七-集成Flask-SocketIO的实时通信系统
后端·python·flask
超级小忍4 小时前
Spring Boot 中常用的工具类库及其使用示例(完整版)
spring boot·后端
CHENWENFEIc4 小时前
SpringBoot论坛系统安全测试实战报告
spring boot·后端·程序人生·spring·系统安全·安全测试