Webservice实现调用外部接口

首先Webservice是干什么的呢?

  • 经过学习可以了解到:webservice是可以跨平台,跨开发语言去实现客户端去调用服务端接口,从而达到数据的交换传输。
  • 有篇文章有句话总结得很好:抄录自https://www.cnblogs.com/xdp-gacl/p/4048937.html(侵删): WebService是一种跨编程语言和跨操作系统平台的远程调用技术。
  • 服务端经过webservice技术把自己的接口暴露出来让有需求的系统得以对接。客户端可以通过账号密码或者某些约定调用服务端的数据。

Webservice有什么内容?:

  • XML+XSD,SOAP和WSDL就是构成WebService平台的三大技术:
  • XML:负责封装返回的数据,XML的跨平台性使它是这个技术的不二之选。XSD:负责定义数据类型。
  • SOAP协议:HTTP协议加XML数据格式。
  • WSDL语言:描述服务端的服务,数据类型,返回格式,通过什么URL,什么方式调用。

Webservice适用的场景:

  • 企业级应用,当不同平台的系统要相互交换数据,推送数据时,利用Webservice可以实现。
  • 例如淘宝等客户群庞大的应用,当外部应用需要对接其中一个接口时,通过Webservice暴露这个接口可以使大量对接需求被满足。
  • 等等

Webservice服务端代码实现

pom依赖:

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.2.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.2.6</version>
        </dependency>

CXF框架使代码在实现调用远程接口的时候更便捷。(需要补充)
接口

@WebService
public interface HelloWord {
    String sayhai(String name);
}

实现接口

@WebService(targetNamespace="http://WebService.demo.example.com/",endpointInterface = "com.example.demo.WebService.HelloWord")
public class HelloWords implements HelloWord {
    @Override
    public  String sayhai(String name){
        return  name+"你好,现在北京时间是"+new Date();
    }

}

暴露服务:

@Configuration
public class WebServiceConfig {
/*Parameter 1 of constructor in org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration required a bean of type 'org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath' that could not be found.
 报错----原因是parent版本造成的问题
 parent版本是1.4.5,同时jaxws版本是3.1.7时;
parent版本是1.4.5,同时jaxws版本是3.1.11时;
parent版本是1.5.6,同时jaxws版本是3.1.11时;
parent版本是1.5.8,同时jaxws版本是3.1.12时;
parent版本是1.5.9,同时jaxws版本是3.1.11时;
parent版本是1.5.9,同时jaxws版本是3.1.12时;
parent版本是2.0.3,同时jaxws版本是3.2.6时;
则运行正常。
把parent版本改成2.0.3,同时jaxws版本是3.2.6时;
 */
/*http://localhost:8080/service/hello?wsdl访问到接口的xml文档*/
    @Bean
    public ServletRegistrationBean dispatcherServlet(){
        return new ServletRegistrationBean(new CXFServlet(),"/service/*");//发布服务名称
    }

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

    @Bean
    public HelloWord userService()
    {
        return  new HelloWords();
    }

    @Bean
    public Endpoint endpoint() {
        System.out.println("Server is starting...");
        EndpointImpl endpoint=new EndpointImpl(springBus(), userService());//绑定要发布的服务
        endpoint.publish("/hello"); //显示要发布的名称
        System.out.println("Server is stop...");
        return endpoint;
    }
}

访问wsdl接口文档;
在这里插入图片描述

通过SOAPUI工具调用接口测试
在这里插入图片描述
这里填上接口文档的地址:
这里填上接口文档的地址
在这里插入图片描述
经过测试说明服务端接口返回正常:
在这里插入图片描述

Webservice客户端代码实现

public class Client {
    public static void main(String args[]) {
        Client.main1();

    }
//调接口有两种方法
public static void main1(){
    try {
        System.out.println("Server is using...");
        JaxWsDynamicClientFactory jaxWsDynamicClientFactory = JaxWsDynamicClientFactory.newInstance();
        org.apache.cxf.endpoint.Client client = jaxWsDynamicClientFactory.createClient("http://localhost:8080/service/hello?wsdl");
        // 需要密码的情况需要加上用户名和密码
                // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));这是访问接口的权限控制
        Object[] contens = client.invoke("sayhai", "小明");//方法名,参数
        /*
        * 操作返回的数据
        * */
        System.out.println(contens[0]);
    } catch (Exception e) {
        System.out.println(e);
    }

}
public  static  void  main2(){
    try {
        String address="http://localhost:8080/service/hello?wsdl";
        //代理工厂
        JaxWsProxyFactoryBean jaxWsProxyFactoryBean=new JaxWsProxyFactoryBean();
        //设置代理地址
        jaxWsProxyFactoryBean.setAddress(address);
        //设置接口类型
        jaxWsProxyFactoryBean.setServiceClass(HelloWord.class);
        //创建一个代理接口实现
        HelloWord helloWord=(HelloWord)jaxWsProxyFactoryBean.create();
        //数据
        String name="小明";
        //调用代理接口的方法调用并返回结果
        String res=helloWord.sayhai(name);
        System.out.println("****************"+res);
    }catch (Exception e){

        System.out.println(e);
    }

}
}
  • 客户端有两种方式去调用服务端的接口,一种是通过代理工厂的方式,一种的动态的创建客户端。

总结:还没有项目可以提供我练手,真正实现起来肯定还会有很多我现在没遇到的问题。按照目前的项目而言,就算有项目需要用到webservice,应该也只是实现客户端调用人家的接口。在查阅资料的时候看了一眼。基于局域网部署的,同一台服务器的两个系统如果要相互调用对方的接口,用webservice实现就不适用了,第一耗能严重,第二有更小更轻便的技术可以去实现。之后要学习的思路不仅限于学了一个组件,一个技术,还要了解和他相近的,是在他之前还是之后出现的技术,他们之间的联系和优缺点。

  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot可以使用RestTemplate来调用外部webservice接口。首先,你需要在SpringBoot中整合RestTemplate。你可以创建一个配置类,使用@Configuration注解标记,并注入RestTemplate bean。在配置类中,你可以设置RestTemplate的一些属性,比如连接超时时间、读取超时时间等。然后,你可以使用RestTemplate的方法来发送HTTP请求,调用外部webservice接口。你可以使用getForObject或postForObject等方法来发送GET或POST请求,并获取返回的结果。在调用webservice接口时,你需要提供接口的URL、请求参数等信息。你可以使用RestTemplate的exchange方法来发送请求,并获取返回的ResponseEntity对象,然后从ResponseEntity对象中获取返回的数据。总之,使用RestTemplate可以方便地调用外部webservice接口。\[1\]如果你觉得使用webservice客户端调用服务器端不方便,或者不会使用webservice客户端,可以尝试使用RestTemplate来调用webservice接口。\[1\]在SpringBoot中整合RestTemplate需要引入相应的依赖,比如spring-boot-starter-web-services和cxf-spring-boot-starter-jaxws等。你可以在项目的pom.xml文件中添加这些依赖。\[3\]然后,你可以创建一个配置类,使用@Configuration注解标记,并注入RestTemplate bean。在配置类中,你可以设置RestTemplate的一些属性,比如连接超时时间、读取超时时间等。\[2\]接下来,你可以使用RestTemplate的方法来发送HTTP请求,调用外部webservice接口。你可以使用getForObject或postForObject等方法来发送GET或POST请求,并获取返回的结果。在调用webservice接口时,你需要提供接口的URL、请求参数等信息。你可以使用RestTemplate的exchange方法来发送请求,并获取返回的ResponseEntity对象,然后从ResponseEntity对象中获取返回的数据。总之,使用RestTemplate可以方便地调用外部webservice接口。 #### 引用[.reference_title] - *1* [基于Springboot整合RestTemplate调用Webservice接口](https://blog.csdn.net/u011652364/article/details/117544660)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [SpringBoot2.3整合WebService实现远程调用](https://blog.csdn.net/liu320yj/article/details/121740367)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值