• webservice初探


    使用jdk1.8完成了一个小示例,记录一下。springboot使用的2.7.15版本。

    服务端

    domain

    package com.example.wsserver.domain;
    
    public class Zonecode {
        
        public Zonecode(String code, String name) {
            this.code = code;
            this.name = name;
        }
    
        private String code;
    
        private String name;
    
        public String getCode() {
            return code;
        }
    
        public void setCode(String code) {
            this.code = code;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        @Override
        public String toString() {
            return "Zonecode{" +
                    "code='" + code + '\'' +
                    ", name='" + name + '\'' +
                    '}';
        }
    }
    
    
    • 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

    服务

    package com.example.wsserver.service;
    
    import com.example.wsserver.domain.Zonecode;
    
    import javax.jws.WebService;
    import java.util.ArrayList;
    import java.util.List;
    
    @WebService
    public class ZonecodeService {
    
        public List<Zonecode> queryCode() {
            List<Zonecode> codes = new ArrayList<>();
    
            Zonecode bj = new Zonecode("110000", "北京市");
            Zonecode tj = new Zonecode("120000", "天津市");
            Zonecode hb = new Zonecode("130000", "河北");
            Zonecode sx = new Zonecode("140000", "山西");
    
            codes.add(bj);
            codes.add(tj);
            codes.add(hb);
            codes.add(sx);
    
            return codes;
        }
    
    }
    
    
    • 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

    开放服务

    package com.example.wsserver;
    
    import com.example.wsserver.service.ZonecodeService;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    import javax.xml.ws.Endpoint;
    
    @SpringBootApplication
    public class WsServerApplication {
    
        public static void main(String[] args) {
    
            SpringApplication.run(WsServerApplication.class, args);
    
            Endpoint.publish(
                    "http://localhost:8081/WebServiceExample/queryCode",
                    new ZonecodeService());
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    服务地址:
    http://localhost:8081/WebServiceExample/queryCode?wsdl

    客户端

    生成代码

    wsimport -keep -p com.example.client.zonecode http://localhost:8081/WebServiceExample/queryCode?wsdl
    
    • 1

    代码结构

    在这里插入图片描述

    测试代码

    @RestController
    public class ZoneController {
    
        @GetMapping("/codes")
        public List<Zonecode> codes() {
            ZonecodeService zonecodeService = new ZonecodeServiceService().getZonecodeServicePort();
    
            return zonecodeService.queryCode();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    报错

    在这里插入图片描述
    经排查是因为名称相同导致,所以需要修改名称,做如下处理:

    修改QueryCode

    在这里插入图片描述

    修改ObjectFactory

    在这里插入图片描述

    修改QueryCodeResponse

    在这里插入图片描述

    成功结果

    在这里插入图片描述

  • 相关阅读:
    速卖通,国际站测评补单用什么环境,买家账号不会被风控,F号
    Python 获取 NCBI 基因名 SSL 证书出现异常?
    Fragment 这些 API 已废弃,你还在使用吗?
    LeetCode 1113.报告的记录
    一、Lua 教程的学习
    useTransition 和 useDeferredValue 初体验
    C/C++数据结构——字典序最小的中序遍历
    压力传感器
    嵌入式Linux driver开发实操(二十二):写一个ALSA驱动程序
    java计算机毕业设计小说阅读网站源程序+mysql+系统+lw文档+远程调试
  • 原文地址:https://blog.csdn.net/lzx5290/article/details/132973912