1分钟速通Webservice服务端和客户端

服务端实现:

我们随便实现一个简单服务,客户请求我们的服务,我们给客户返回响应的信息

@WebService
public class HelloServiceImpl implements HelloService {
    @Override
    public String getString(String name) {
        return "hello," + name;
    }
}

这里用到了一个关键的注解@WebService表示该类是一个服务类,需要发布其中的public的方法

发布服务,Endpoint类发布服务,publish方法,两个参数:1.服务地址;2.服务实现类

package com.zlp.server;

import com.zlp.service.impl.HelloServiceImpl;
import org.springframework.context.SmartLifecycle;
import org.springframework.stereotype.Component;

import javax.xml.ws.Endpoint;

@Component
public class HelloServer implements SmartLifecycle {

    @Override
    public void start() {
        // spring容器启动之后执行当前方法内逻辑
        System.out.println("webservice start");
        Endpoint.publish("http://127.0.0.1:8808/hello", new HelloServiceImpl());
    }

    @Override
    public void stop() {
        System.out.println("webservice end");
    }

    @Override
    public boolean isRunning() {
        return false;
    }
}

测试访问wsdl:

客户端实现:

1、直接通过命令行发送请求命令,webservice本质就http请求

2、Java代码实现:

package com.zlp.client;


import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class HelloClient {
    public static void main(String[] args) throws Exception {
        String requestSoapMessage = getRequestSoapMessage("webservice");
        //System.out.println(requestSoapMessage);
        HttpClient client = new HttpClient();
        PostMethod postMethod = new PostMethod("http://127.0.0.1:8808/hello?wsdl");
        postMethod.setRequestBody(requestSoapMessage);
        client.executeMethod(postMethod);
        String xml = postMethod.getResponseBodyAsString();
        System.out.println(xml);

        // 解析xml数据
        InputStream in = new ByteArrayInputStream(xml.getBytes("utf-8"));
        SAXReader sax = new SAXReader();
        Document document = sax.read(in);
        Element root = document.getRootElement();
        Element e = root.element("Body").element("getStringResponse").element("return");
        System.out.println("result:" + e.getData().toString());
    }

    private static String getRequestSoapMessage(String name) {
        String requestSoapMessage = "";
        StringBuffer buf = new StringBuffer();
        buf.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>")

                .append("\n").append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:impl=\"http://impl.service.zlp.com/\"> ")
                .append("\n").append("<soapenv:Header />")
                .append("\n").append("<soapenv:Body>")
                .append("\n").append("<impl:getString>")
                .append("\n").append("<arg0>"+name+"</arg0>")
                .append("\n").append("</impl:getString>")
                .append("\n").append("</soapenv:Body>")
                .append("\n").append("</soapenv:Envelope>");

        requestSoapMessage = buf.toString();
        return requestSoapMessage;
    }
}

执行结果:

3、SoapUI客户端工具

输入wsdl地址:

相关推荐

  1. GRPC服务客户DEMO

    2024-01-11 23:30:03       43 阅读
  2. Qt建立服务客户

    2024-01-11 23:30:03       35 阅读
  3. 客户渲染与服务渲染(1

    2024-01-11 23:30:03       39 阅读
  4. udp(无连接)客户服务代码

    2024-01-11 23:30:03       69 阅读
  5. 04 使用gRPC实现客户服务通信

    2024-01-11 23:30:03       50 阅读
  6. netty的TCP服务客户实现

    2024-01-11 23:30:03       50 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-11 23:30:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-11 23:30:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-01-11 23:30:03       82 阅读
  4. Python语言-面向对象

    2024-01-11 23:30:03       91 阅读

热门阅读

  1. C++之优先队列(priority_queue)

    2024-01-11 23:30:03       51 阅读
  2. 笔记-卓有成效的管理者the effective executive

    2024-01-11 23:30:03       64 阅读
  3. VCG 网格添加与删除顶点或面片

    2024-01-11 23:30:03       65 阅读
  4. Elasticsearch入门

    2024-01-11 23:30:03       55 阅读
  5. h5 支付宝支付/微信支付

    2024-01-11 23:30:03       49 阅读
  6. excel 按照姓名日期年份分组求和

    2024-01-11 23:30:03       49 阅读
  7. 极智AI | 谈谈AI发展系列汇总

    2024-01-11 23:30:03       62 阅读
  8. Autosar通信实战系列11-PduR模块要点及其配置介绍

    2024-01-11 23:30:03       60 阅读
  9. python的猴子补丁(Monkey Patching)

    2024-01-11 23:30:03       55 阅读
  10. jQuery入门

    2024-01-11 23:30:03       49 阅读
  11. Linux 开启Swap交换内存

    2024-01-11 23:30:03       59 阅读
  12. linux—多服务免密登录

    2024-01-11 23:30:03       48 阅读
  13. js获取光标坐标

    2024-01-11 23:30:03       55 阅读
  14. docker镜像版本号规则定义

    2024-01-11 23:30:03       48 阅读