微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用PostMan测试WebService服务

使用PostMan测试WebService服务

1、配置环境

官网下载CXF:

https://cxf.apache.org/download.html

项目中引入CXF依赖

# 1.解压下载的压缩包
# 2.引入CXF相关依赖
- 将文件夹下的lib文件夹复制到java项目下的/web/WEB-INF/中
- 将lib文件夹导入项目的依赖中

2、编写WebService接口和实现类

接口

@WebService
public interface HelloWorld {

    public String sayHello(@WebParam(name = "name", targetNamespace = "http://test.hao.com/") String name, @WebParam(name = "age", targetNamespace = "http://test.hao.com/") int age);
}

实现类

public class HelloWorldImpl implements HelloWorld {

    @Override
    public String sayHello(String name, int age) {
        return "cxf:" + name + "\t" + age;
    }
}

3、将编写好的接口添加到server

public class MainServer {
    public static void main(String[] args) {

        JaxWsServerfactorybean server = new JaxWsServerfactorybean();

        server.setAddress("http://localhost/cxf/hello");
        server.setServiceClass(HelloWorldImpl.class);

        server.create();

        server.setStart(true);
    }
}

4、使用PostMan测试接口

# 1.在浏览器中输入http://localhost/cxf/hello?wsdl查看是否启动服务

image-20210726122301351

# 2.打开postman进行接口测试
- 1.打开postman,新建一个Request请求
- 2.选择请求方式为POST ,设置headers为:Content-Type  text/xml;charset=utf-8

image-20210726122550170

# 3.postman设置请求参数
- 浏览器打开地址 http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl 找空间命名,这个位置是固定的。这个下面会用到,这里是 `http://test.hao.com/`

image-20210726122928891

# 4.设置参数的具体信息

image-20210726123109872

# 5.设置body
- 进入body框,选择raw,xml,如下图

image-20210726123343265

# 6.填入请求的xml

格式:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>

  </soap:Body>
</soap:Envelope>

举例:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <sayHello xmlns="http://test.hao.com/">
          <name>张三</name>
      <age>19</age>
    </sayHello>
  </soap:Body>
</soap:Envelope>

5、发送请求

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐