zoukankan      html  css  js  c++  java
  • springboot整合webservice

    一、webservice知识补充

    简单来说,webservice就是远程调用技术,也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的独立的通讯技术。是:通过SOAP在Web上提供的软件服务,使用WSDL文件进行说明,并通过UDDI进行注册。

    • XML(Extensible Markup Language)

      扩展型可标记语言。面向短期的临时数据处理、面向万维网络,是Soap的基础。

    • Soap(Simple Object Access Protocol)

      简单对象存取协议。是XML Web Service
      的通信协议。当用户通过UDDI找到你的WSDL描述文档后,他通过可以SOAP调用你建立的Web服务中的一个或多个操作。SOAP是XML文档形式的调用方法的规范,它可以支持不同的底层接口,像HTTP(S)或者SMTP。

    • WSDL(Web Services Description Language)

      WSDL 文件是一个 XML 文档,用于说明一组 SOAP 消息以及如何交换这些消息。大多数情况下由软件自动生成和使用

    • UUID(Universal Description, Discovery and Integration)

      UDDI[1] 是一种规范,它主要提供基于Web服务的注册和发现机制,为Web服务提供三个重要的技术支持:①标准、透明、专门描述Web服务的机制;②调用Web服务的机制;③可以访问的Web服务注册中心。UDDI规范由OASIS(Organization
      for the Advancement of Structured Information Standards[1] )标准化组织制定。[1]

    二、集成webservice

    1. 引用依赖

      <dependency>
         <groupId>org.apache.cxf</groupId>
         <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
         <version>3.2.4</version>
      </dependency>
      
    2. 编写Service接口

      package top.xtslife.mywebservice.service;
      
      import javax.jws.WebMethod;
      import javax.jws.WebParam;
      import javax.jws.WebService;
      
      /**
       * @Author 小涛
       * @Create 2019/8/19 13:52
       */
      @WebService
      public interface WebServiceDemoService {
          @WebMethod  //标注该方法为webservice暴露的方法,用于向外公布,它修饰的方法是webservice方法,去掉也没影响的,类似一个注释信息。
          String hello(@WebParam(name="name") String name);
      }
      
    3. 编写Service实现类

      package top.xtslife.mywebservice.service.impl;
      
      import org.springframework.stereotype.Service;
      import top.xtslife.mywebservice.service.WebServiceDemoService;
      
      import javax.jws.WebService;
      
      /**
       * @Author 小涛
       * @Create 2019-08-19  13:54
       */
      @Service
      //name暴露的服务名称, targetNamespace:命名空间,设置为接口的包名倒写(默认是本类包名倒写). endpointInterface接口地址
      @WebService(serviceName = "WebServiceDemoService",
                  targetNamespace = "http://service.mywebservice.xtslife.top",
                  endpointInterface = "top.xtslife.mywebservice.service.WebServiceDemoService")
      public class WebServiceDemoServiceIpml implements WebServiceDemoService {
          @Override
          public String hello(String name) {
              return "hello"+name;
          }
      }
      
      
    4. 注册服务

      package top.xtslife.mywebservice.config;
      
      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 top.xtslife.mywebservice.service.WebServiceDemoService;
      
      import javax.jws.WebService;
      import javax.servlet.ServletRegistration;
      import javax.xml.ws.Endpoint;
      
      /**
       * @Author 小涛
       * @Create 2019-08-19  13:58
       */
      @Configuration
      public class WebServieConfig {
          @Autowired
          private WebServiceDemoService webServiceDemoServiceImpl;
      
          @SuppressWarnings("all")
           //默认servlet路径/*,如果覆写则按照自己定义的来
          @Bean(name = "cxfServlet")
          public ServletRegistrationBean cxfServlet() {
              return new ServletRegistrationBean(new CXFServlet(),"/webservice/*");
          }
      
          @Bean(name = Bus.DEFAULT_BUS_ID)
          public SpringBus springBus() {
              return new SpringBus();
          }
      
          /**
           * 注册WebServiceDemoService接口到webservice服务
           * @return
           */
          @Bean(name = "WebServiceDemoEndpoint")
          public Endpoint sweptPayEndpoint() {
              EndpointImpl endpoint = new EndpointImpl(springBus(),webServiceDemoServiceImpl);
              endpoint.publish("/mywebservice");
              return endpoint;
          }
      
      }
      
    5. 调用webservice

      注:要拿到接口

      package top.xtslife.mywebservice.webserviceclient;
      
      import org.apache.cxf.endpoint.Client;
      import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
      import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
      import top.xtslife.mywebservice.service.WebServiceDemoService;
      
      /**
       * @Author 小涛
       * @Create 2019-08-19  15:12
       */
      public class CxfClient {
          public static void main(String[] args) {
              callService();
          }
      
          public static void callService(){
              try {
                  // 接口地址
                  String address = "http://localhost:8080/webservice/mywebservice?wsdl";
                  // 代理工厂
                  JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
                  // 设置代理地址
                  jaxWsProxyFactoryBean.setAddress(address);
                  // 设置接口类型
                  jaxWsProxyFactoryBean.setServiceClass(WebServiceDemoService.class);
                  // 创建一个代理接口实现
                  WebServiceDemoService webServiceDemoServiceimpl = (WebServiceDemoService) jaxWsProxyFactoryBean.create();
                  // 准备数据
                  String name = "张涛";
                  String result = webServiceDemoServiceimpl.hello(name);
                  System.out.println("返回结果:" + result);
              }catch (Exception e){
                  e.printStackTrace();
              }
          }
      }
      
      

      参考文章:springBoot + webservice:https://www.jianshu.com/p/dd1bf8fa6904



    作者:关小涛
    学习和分享是博客最大的乐趣,欢迎大家取之所需。
    努力是自己努力的原因,每周天写博客总结工作中的新技能和出现的问题
  • 相关阅读:
    OCP-1Z0-053-V12.02-622题
    OCP-1Z0-053-V12.02-501题
    Flex实现查询和重置
    闪回事务处理回退
    闪回数据归档测试
    闪回数据归档
    OCP-1Z0-053-V12.02-166题
    VC-摄像头控制SDK源码
    MFC 的 Picture Control 加载 BMP/PNG 图片的方法
    OCP-1Z0-051-V9.02-25题
  • 原文地址:https://www.cnblogs.com/XtsLife/p/11377909.html
Copyright © 2011-2022 走看看