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