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;
}

🍚总结

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

相关推荐
FreeBuf_4 小时前
黄金旋律IAB组织利用暴露的ASP.NET机器密钥实施未授权访问
网络·后端·asp.net
张小洛5 小时前
Spring AOP 是如何生效的(入口源码级解析)?
java·后端·spring
why技术6 小时前
也是出息了,业务代码里面也用上算法了。
java·后端·算法
白仑色8 小时前
完整 Spring Boot + Vue 登录系统
vue.js·spring boot·后端
ZhangApple9 小时前
微信自动化工具:让自己的微信变成智能机器人!
前端·后端
Codebee9 小时前
OneCode 3.0: 注解驱动的Spring生态增强方案
后端·设计模式·架构
bobz9659 小时前
kubevirt virtinformers
后端
LuckyLay9 小时前
Django专家成长路线知识点——AI教你学Django
后端·python·django
Java微观世界9 小时前
征服Java三大特性:封装×继承×多态+this/super高阶指南
后端
Java技术小馆9 小时前
RPC vs RESTful架构选择背后的技术博弈
后端·面试·架构