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

 更新时间:2023年10月20日 10:21:28   作者:知识浅谈  
这篇文章主要介绍了SpringBoot WebService服务端&客户端使用案例教程,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

服务端:

依赖

<!--        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>

服务接口

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

服务接口实现类

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

cxf配置类

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

客户端

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

到此这篇关于SpringBoot WebService服务端&amp;客户端使用教程的文章就介绍到这了,更多相关SpringBoot WebService服务端内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • WebSocket实现聊天室业务

    WebSocket实现聊天室业务

    这篇文章主要为大家详细介绍了WebSocket实现聊天室业务,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-08-08
  • Dubbo之降级Mock源码分析

    Dubbo之降级Mock源码分析

    这篇文章主要为大家介绍了Dubbo降级Mock源码分析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-09-09
  • SpringBoot整合BCrypt实现密码加密

    SpringBoot整合BCrypt实现密码加密

    这篇文章主要为大家详细介绍了SpringBoot整合BCrypt进行密码加密,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-11-11
  • JeecgBoot代码生成器自定义请求接口实例

    JeecgBoot代码生成器自定义请求接口实例

    文章介绍了如何在使用代码生成器生成的页面中添加启用/停用功能,通过修改List.vue页面并添加必要的请求头token来解决身份认证失败的问题,同时,还讲述了如何在菜单管理、角色管理中进行权限配置,以确保按钮功能生效
    2026-01-01
  • SpringBoot发送异步邮件流程与实现详解

    SpringBoot发送异步邮件流程与实现详解

    这篇文章主要介绍了SpringBoot发送异步邮件流程与实现详解,Servlet阶段邮件发送非常的复杂,如果现代化的Java开发是那个样子该有多糟糕,现在SpringBoot中集成好了邮件发送的东西,而且操作十分简单容易上手,需要的朋友可以参考下
    2024-01-01
  • Java异常类型及处理详情

    Java异常类型及处理详情

    这篇文章主要介绍了Java异常类型及处理, 异常指的是程序在执行过程中,出现了非正常情况,导致了java的jvm停止。感兴趣的小伙伴就和小编一起来学习下面文章的具体内容吧
    2021-09-09
  • java 嵌套类的详解及实例代码

    java 嵌套类的详解及实例代码

    这篇文章主要介绍了java 嵌套类的详解及实例代码的相关资料,需要的朋友可以参考下
    2017-03-03
  • vue验证码组件应用实例

    vue验证码组件应用实例

    今天小编就为大家分享一篇关于vue验证码组件应用实例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-01-01
  • Spring条件注解@Conditional示例详解

    Spring条件注解@Conditional示例详解

    这篇文章主要给大家介绍了关于Spring条件注解@Conditional的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Spring具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-08-08
  • 如何在Java中优雅地使用正则表达式详解

    如何在Java中优雅地使用正则表达式详解

    这篇文章主要给大家介绍了关于如何在Java中优雅地使用正则表达式的相关资料,正则表达式就是一个字符串,但和普通的字符串不同的是,正则表达式是对一组相似字符串的抽象,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-02-02

最新评论