zoukankan      html  css  js  c++  java
  • Spring boot webService使用

    以前一家公司,项目用到webservice,不过后来没待多久,当时也要弄别的也就没有研究,

    这次也遇到过这样一个使用场景,需要对接别人的一个人脸识别服务,在什么都没有的情况下,对方只给了一个wsdl的地址过来,全程都靠自己去研究了.

    先就webservice 讲下自己的理解把,感觉有点像websockt ,它可以实现一个服务端, 然后在客户端去调用服务端去完成服务端的操作.

    这里使用spring-boot

    1.先创建spring-boot项目,引入jar包

    <!-- web Services -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web-services</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
                <version>3.2.7</version>
            </dependency>
    

    2.创建一个对象.

    package com.sunzy.mywebservice;
    
    
    import lombok.Getter;
    import lombok.Setter;
    
    /**auth : szy
     *time : 2020-01-03
     **/
    @Getter
    @Setter
    public class Person {
        private Integer id;
        private String name;
        private String niceName;
        private Integer age;
        private Double height;
    }

    创建一个服务端接口

    package com.sunzy.mywebservice.service;
    
    import com.sunzy.mywebservice.Person;
    
    import javax.jws.WebMethod;
    import javax.jws.WebParam;
    import javax.jws.WebService;
    
    /**
     * @author :szy
     * @title: TestApiService
     * @projectName mywebservice
     * @description: TODO
     * @date 2020/1/3-15:56
     */
    @WebService(targetNamespace = "http://service.mywebservice.sunzy.com")
    public interface TestApiService {
    
        /**
         * 解析出的地址  http://localhost:8080/ws/testApiService?wsdl
         * @param person
         * @return
         */
        @WebMethod
        Person insertPersonInfo(@WebParam(name = "PERSON", targetNamespace = "http://service.mywebservice.sunzy.com") String person);
    }

    服务端的实现方法

    package com.sunzy.mywebservice.service.Impl;
    
    import com.alibaba.fastjson.JSONArray;
    import com.sunzy.mywebservice.Person;
    import com.sunzy.mywebservice.service.TestApiService;
    import org.springframework.stereotype.Component;
    
    import javax.jws.WebService;
    import java.util.List;
    
    /**auth : szy
     *time : 2020-01-03
     **/
    @Component
    @WebService(name = "testApiService",
            targetNamespace = "http://service.mywebservice.sunzy.com",
            endpointInterface = "com.sunzy.mywebservice.service.TestApiService",
            portName = "10000")
    public class TestApiServiceImpl implements TestApiService {
        @Override
        public Person insertPersonInfo(String person) {
            System.out.println("服务端接口到了请求:person="+person);
            List<Person> list = JSONArray.parseArray(person, Person.class);
            //TODO 逻辑处理
            return list.get(0);
        }
    }

    配置文件,将服务进行开放出去,给外部使用.

    到这里已经完成了,运行项目.访问地址:http://localhost:8080/ws/testApiService?wsdl

     能输出下面,表示服务端部署成功了.

    那么下面是客户端如何去访问

    可以新建一个项目,这里采用本项目去调用.用idea 去解析wsdl,生成对应的代码.

    选择项目

     通过wsdl,生成java代码.

     上面填生成的地址,下面试填写包名,重点 这里上面的地址是要有效能访问到的,不然程序是读取不到东西的,更不要说解析了

    生成代码完成后,可以看到代码:

     调用方法,这里就自己写一个控制器,模仿下客户端去调用.

    package com.sunzy.mywebservice.controller;/**
     * @title: Hello
     * @projectName mywebservice
     * @description: TODO
     * @author :szy
     * @date 2020/1/3-15:44
     */
    
    import com.sunzy.mywebservice.Person;
    import com.sunzy.mywebservice.config.TestApiServiceImplService;
    import com.sunzy.mywebservice.service.TestApiService;
    import org.apache.cxf.endpoint.Client;
    import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
    import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.net.URL;
    import java.util.Map;
    
    /**auth : szy
     *time : 2020-01-03
     **/
    @RestController
    @RequestMapping("/adminWebservice")
    public class Hello {
    
        // 获取单位信息
        @GetMapping(value="/sync")
        public String sync(@RequestParam(value="data") String data) throws Exception{
    
            // 接口地址
                         String address = "http://localhost:8080/ws/testApiService?wsdl";
                         // 代理工厂
                         JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
                         // 设置代理地址
                         jaxWsProxyFactoryBean.setAddress(address);
                         // 设置接口类型
                         jaxWsProxyFactoryBean.setServiceClass(TestApiService.class);
                         // 创建一个代理接口实现
                        TestApiService us = (TestApiService) jaxWsProxyFactoryBean.create();
    
                         // 数据准备
                         String userId = "[{"name":"JACK"},{"name":"TOM"}]";
                         // 调用代理接口的方法调用并返回结果
                         Person person = us.insertPersonInfo(userId);
                         System.out.println("返回结果:" + person.toString());
    
            return "index";
        }
    
    
    
        // 动态调用 外部调用
        @GetMapping(value="/dontest")
        public String dontest(@RequestParam(value="data") String data) throws Exception{
    
            JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
            Client client = dcf.createClient("http://127.0.0.1:8080/ws/testApiService?wsdl");
    
    
            Object[] objects = new Object[0];
    
            try {
                // invoke("方法名",参数1,参数2,参数3....);
                // 数据准备
                String userId = "[{"name":"JACK"},{"name":"TOM"}]";
                objects = client.invoke("insertPersonInfo", userId);
    
                System.out.println("返回数据:" + objects[0]);
    
            } catch (java.lang.Exception e) {
                e.printStackTrace();
            }
    
            return "index";
        }
    
    
        // 动态调用 外部调用(外部模拟客服端调用服务端)
        @GetMapping(value="/dontest2")
        public String dontest2(@RequestParam(value="data") String data) throws Exception{
    
            //调用服务端
            TestApiServiceImplService serviceImplService = new TestApiServiceImplService();
    
            com.sunzy.mywebservice.config.TestApiService apiService = serviceImplService.get10000();
            String userId = "[{"name":"小红"},{"name":"小蓝"}]";
    
            com.sunzy.mywebservice.config.Person x =  apiService.insertPersonInfo(userId);
    
            //服务端返回的数据
            System.out.println("返回数据:" + x.toString());
    
            return "index";
        }
    }

    通过postman可以看到调用成功.

  • 相关阅读:
    ElasticSearch实战系列一: ElasticSearch集群+Kinaba安装教程
    SpringBoot事物Transaction实战讲解教程
    SpringBoot整合Swagger和Actuator
    SpringBoot项目实现文件上传和邮件发送
    SpringBoot优雅的全局异常处理
    SpringCloud学习系列之七 ----- Zuul路由网关的过滤器和异常处理
    SpringBoot整合Redis使用Restful风格实现CRUD功能
    SpringCloud学习系列之六 ----- 路由网关Zuul基础使用教程
    SpringCloud学习系列之五-----配置中心(Config)和消息总线(Bus)完美使用版
    SpringCloud学习系列之四-----配置中心(Config)使用详解
  • 原文地址:https://www.cnblogs.com/sunxun/p/12171814.html
Copyright © 2011-2022 走看看