1. 程式人生 > 其它 >使用PostMan測試WebService服務

使用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檢視是否啟動服務
# 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、傳送請求