使用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