首页 > Web开发 > 详细

使用PostMan测试WebService服务

时间:2021-07-26 14:45:28      阅读:16      评论:0      收藏:0      [点我收藏+]

使用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查看是否启动服务

技术分享图片

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

技术分享图片

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

技术分享图片

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

技术分享图片

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

技术分享图片

# 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、发送请求

使用PostMan测试WebService服务

原文:https://www.cnblogs.com/nuoxin/p/15060905.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!