• spring boot 使用 webservice


    spring boot 使用 webservice

    使用 java 自带的 jax-ws

    依赖

    如果是jdk1.8,不需要引入任何依赖,如果大于1.8

    <dependency>
        <groupId>javax.jwsgroupId>
        <artifactId>javax.jws-apiartifactId>
        <version>1.1version>
    dependency>
    <dependency>
        <groupId>javax.xml.wsgroupId>
        <artifactId>jaxws-apiartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    定义服务接口

    使用 @WebService 注解

    @WebService
    @SOAPBinding(style = SOAPBinding.Style.RPC)
    public interface HumanService {
    
        @WebMethod
        public boolean addHuman(Human human);
    
        @WebMethod
        public boolean deleteHuman(String name);
    
        @WebMethod
        public Human get(String name);
    
        /**
         * 不能处理List, 只能处理数组
         * @return
         */
        @WebMethod
        public Human[] getAll();
        
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    实现

    package com.example.demoweb.webservice.impl;
    
    import com.example.demoweb.model.Human;
    import com.example.demoweb.webservice.HumanService;
    import org.springframework.util.CollectionUtils;
    
    import javax.jws.WebService;
    import java.util.*;
    
    /**
     * @Author: xiaodong.zheng
     * @Date: 2024/3/7 15:36
     */
    @WebService(endpointInterface = "com.example.demoweb.webservice.HumanService",
            serviceName = "HumanService",
            targetNamespace = "human.ns" //随便写,不过在客户端调用时会用到
    )
    public class HumanServiceImpl implements HumanService {
    
    
        private Map<String, Human> data = new HashMap<>();
    
        @Override
        public boolean addHuman(Human human) {
            data.put(human.getName(), human);
            return true;
        }
    
        @Override
        public boolean deleteHuman(String name) {
            data.remove(name);
            return true;
        }
    
        @Override
        public Human get(String name) {
            return data.get(name);
        }
    
        @Override
        public Human[] getAll() {
            if (CollectionUtils.isEmpty(data)) {
                return null;
            }
            Human[] hs = new Human[data.size()];
            int i = 0;
            for (Map.Entry<String, Human> entry : data.entrySet()) {
                hs[i] = entry.getValue();
                i++;
            }
            return hs;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    暴露端口

    
    
    @SpringBootApplication
    public class DemoWebApplication {
    
        public static final String WS_HUMAN = "http://localhost:8888/ws/hh";
    
        public static void main(String[] args) {
            //注意webservice服务发布会优先于spring容器启动,不然 使用依赖注入会失败!!
            Endpoint.publish(WS_HUMAN, new HumanServiceImpl());
            SpringApplication.run(DemoWebApplication.class, args);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    访问 http://localhost:8888/ws/hh?wsdl

    在这里插入图片描述

    webservice客户端调用

    动态调用

    @RestController
    public class HumanWebServiceClientController {
    
        @GetMapping("/index")
        public String invoke() throws MalformedURLException {
            //public static final String WS_HUMAN = "http://localhost:8888/ws/hh";
            URL wsdlURL = new URL(WS_HUMAN + "?wsdl");
            //默认localPart=实现类+Service
            QName qname = new QName("human.ns", "HumanService");
            Service service = Service.create(wsdlURL, qname);
            HumanService humanService = service.getPort(HumanService.class);
            Human human = new Human();
            human.setName("tom");
            human.setAge(12);
            boolean b = humanService.addHuman(human);
            System.out.println("add human: " + b);
            Human[] all = humanService.getAll();
            System.out.println("get all data: " + JSON.toJSONString(all));
            return "success";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    依赖注入调用

    使用 @WebServiceClient 注解, 调用者客户端:

    @Component
    @WebServiceClient(targetNamespace = "human.ns", wsdlLocation = WS_HUMAN + "?wsdl",name = "HumanService")
    public class HumanServiceClient extends Service implements HumanService {
    
        public final static QName SERVICE = new QName("human.ns", "HumanService");
    
        public HumanServiceClient() throws MalformedURLException {
            super(new URL(WS_HUMAN + "?wsdl"), SERVICE);
        }
    
    
        @Override
        public boolean addHuman(Human human) {
            return super.getPort(HumanService.class).addHuman(human);
        }
    
        @Override
        public boolean deleteHuman(String name) {
            return super.getPort(HumanService.class).deleteHuman(name);
        }
    
        @Override
        public Human get(String name) {
            return super.getPort(HumanService.class).get(name);
        }
    
        @Override
        public Human[] getAll() {
            return super.getPort(HumanService.class).getAll();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    调用

    @RestController
    public class HumanWebServiceClientController {
    
        @Autowired
        private HumanServiceClient humanServiceClient;
    
        @GetMapping("/index2")
        public String invoke2() throws MalformedURLException {
            Human human = new Human();
            human.setName("tom");
            human.setAge(13);
            boolean b = humanServiceClient.addHuman(human);
            System.out.println("add human: " + b);
            Human[] all = humanServiceClient.getAll();
            System.out.println("get all data: " + JSON.toJSONString(all));
            return "success";
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    结果:

    add human: true
    get all data: [{"age":13,"name":"tom"}]
    
    • 1
    • 2

    good luck!

  • 相关阅读:
    【mia】rtcdn-draft 基于http的rtc订阅及mia实现
    计算机网络重点概念整理-第二章 物理层【期末复习|考研复习】
    nodejs+java+python网上体育用品销售系统
    数据开发流程及规范
    Dapper处理多个结果集与多重映射实例
    阿里云使用 ExternalDNS 集成外部DNS服务
    AutoCAD 2022安装及激活
    ZooKeeper的权限控制--ACL
    如何监控电动车充电桩能耗?
    【操作系统】总结(二)linux指令
  • 原文地址:https://blog.csdn.net/u013887008/article/details/136565615