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地址:

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1个凡夫俗子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值