共計(jì) 778 個(gè)字符,預(yù)計(jì)需要花費(fèi) 2 分鐘才能閱讀完成。
方式一:使用 CXF 提供的工具生成客戶端代碼。
- 在項(xiàng)目中添加 CXF 的依賴。
- 使用 CXF 提供的命令行工具生成客戶端代碼,命令如下:
wsdl2java -d -p
其中,為生成的代碼存放的目錄,為生成的代碼所在的包名,為 WebService 的 WSDL 地址。
- 將生成的代碼引入到項(xiàng)目中,然后就可以調(diào)用 WebService 接口了。例如:
HelloWorldService service = new HelloWorldService();
HelloWorldPortType port = service.getHelloWorldPort();
String result = port.sayHello("World");
System.out.println(result);
方式二:手動(dòng)編寫調(diào)用代碼。
- 創(chuàng)建
JaxWsProxyFactoryBean實(shí)例,并設(shè)置 WebService 的地址。
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setAddress();
- 設(shè)置 WebService 接口的類。
factory.setServiceClass(HelloWorldPortType.class);
- 創(chuàng)建 WebService 接口的代理對(duì)象。
HelloWorldPortType port = (HelloWorldPortType) factory.create();
- 調(diào)用 WebService 接口的方法。
String result = port.sayHello("World");
System.out.println(result);
注意:以上代碼中的 HelloWorldPortType 為 WebService 接口的類名。具體的類名需要根據(jù)生成的客戶端代碼或者 WebService 的定義進(jìn)行調(diào)整。
丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!
正文完
發(fā)表至: Java
2023-12-20