springboot整合WebService

一.webService是什么

  • WebService是一种跨编程语言、跨操作系统平台的远程调用技术。

  • 先对两个名词进行解释:

    • SOAP
      • SAOP是一种WebService平台技术
      • SOAP协议 = HTTP协议 + XML数据格式
      • WebService通过HTTP协议发送请求和接收结果时,发送的请求内容和结果内容都采用XML格式封装, 并增加了一些特定的HTTP消息头,以说明HTTP消息的内容格式,这些特定的HTTP消息头和XML内容格式就是SOAP协议。
      • 当然除了SAOP还有其他WebService技术,XML+XSD,SOAP和WSDL就是构成WebService平台的三大技术。
    • WSDL
      • 好比我们去商店买东西,首先要知道商店里有什么东西可买,然后再来购买,商家的做法就是张贴广告海报。 WebService也一样,WebService客户端要调用一个WebService服务,首先要有知道这个服务的地址在哪,以及这个服务里有什么方法可以调用,所以,WebService务器端首先要通过一个WSDL文件来说明自己家里有啥服务可以对外调用,服务是什么(服务中有哪些方法,方法接受的参数是什么,返回值是什么),服务的网络地址用哪个url地址表示,服务通过什么方式来调用。

      • WSDL(Web Services Description Language)就是这样一个基于XML的语言,用于描述Web Service及其函数、参数和返回值。

      • 一些最新的开发工具既能根据你的Web service生成WSDL文档,又能导入WSDL文档,生成调用相应WebService的代理类代码。

      • WSDL文件保存在Web服务器上,通过一个url地址就可以访问到它。客户端要调用一个WebService服务之前,要知道该服务的WSDL文件的地址。WebService服务提供商可以通过两种方式来暴露它的WSDL文件地址:1.注册到UDDI服务器,以便被人查找;2.直接告诉给客户端调用者。

  • webService是什么:

    • 远程调用技术:远程调用是指一台设备上的程序A可以调用另一台设备上的方法B。比如:银联提供给商场的 pos刷卡系统,商场的pos机转账调用的转账方法的代码其实是跑在银行服务器上的。再比如,amazon,天气预报系统,淘宝网,校内网,百度等把自己的系统服务以WebService服务的形式暴露出来,让第三方网站和程序可以调用这些服务功能,这样扩展了自己系统的市场占有率。
    • 跨编程语言:是指服务端、客户端程序的编程语言可以不同
    • 跨操作系统平台:是指服务端、客户端可在不同的操作系统上运行
    • 从表面上看,WebService是指一个应用程序向外界暴露了一个能通过Web调用的API接口,我们把调用这个WebService的应用程序称作客户端,把提供这个WebService的应用程序称作服务端。
    • 从深层上看,WebService是建立可互操作的分布式应用程序的新平台,是一个平台,是一套标准。它定义了应用程序如何通过Web实现互操作性,通过WebService标准对服务进行查询和访问。

二.怎么实现,这里以springboot为例

环境:  jdk8,	springboot2.2.2.RELEASE,  maven 3.5.0

1.pom引入最新的cxf包


        <!-- CXF webservice -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.3.4</version>
        </dependency>

2.定义SEI接口(我理解就是普通的暴露出去的接口)

import javax.jws.WebService;

import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;

/**SEI<Service Endpoint Interface 发布的服务接口>
 * bindingType:支持soap12协议
 * @date 2020-1-2
 * @params
 * @return
 * @author hex
 */
@WebService
@BindingType(SOAPBinding.SOAP12HTTP_BINDING)
public interface IHelloService {

    String sayHello(String name);
}

3.定义实现类:


import org.springframework.stereotype.Component;

/**实现类必须制定命名空间和sei接口
 * @Date 2020-1-2 15:29
 * @Author hex
 * @Desc
 */
@Component
@WebService(targetNamespace = "http://testservice.webservice.hex.com/",
    endpointInterface = "com.hex.webservice.testservice.IHelloService"
)
public class HelloServiceImpl implements IHelloService {
    @Override
    @WebMethod
    public String sayHello(String name) {
        return "hello"+name;
    }
}
`
4.配置config类,定义调用路径

```java
mport org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

/**
 * @Date 2020-1-2 16:21
 * @Author hex
 * @Desc
 */
@Configuration
public class CxfConfig {
    @Autowired
    private IHelloService helloService;

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

    /**定义webservice访问路径,主路径在application.properties中配置
     * @date 2020-1-2
     * @params  []
     * @return  javax.xml.ws.Endpoint
     * @author hex
     */
    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), helloService);
        endpoint.publish("/api");
        return endpoint;
    }
}

5.配置application文件cxf.path,让spring知道哪些路径是可以调用的.

server.port=8060

cxf.path=/hex

运行后直接浏览器调用显示一段xml代表成功:
http://localhost:8060/hex/api?wsdl

在这里插入图片描述
如果需暴露多个接口,只需要在config中配置多个endPoint类即可

三.如何服务端调用.

public static void main(String[] args) {
            // 创建动态客户端
            JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
            Client client = dcf.createClient("http://localhost:8060/hex/api?wsdl");
            // 需要密码的情况需要加上用户名和密码
            // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
            Object[] objects;
            try {
                // invoke("方法名",参数1,参数2,参数3....);
                objects = client.invoke("sayHello", "hex");
                System.out.println("返回数据:" + objects[0]);
            } catch (java.lang.Exception e) {
                e.printStackTrace();
            }
    }

返回数据
[外链图片转存失败,源站可能有防盗在这里插fa入!链机制保存下来直接描述]-https://Muwblog.csdnimg.cn/20200257731233.pnhttps://img-返回数据blog.csdnimg.cn/20200102172731833.png)
**

github地址:https://github.com/aplqik/webservice_test

**
欢迎转载和分享,如果帮到您了就帮忙点个赞或者互关一下呗!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值