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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM