一、JAX-WS方式調用Webservice介面
JAX-WS是Java API for XML Web Services的縮寫,是一種比較常見的調用Webservice介面的方式。
首先需要在pom.xml文件中配置JAX-WS依賴:
<dependency> <groupId>javax.xml.ws</groupId> <artifactId>javax.xml.ws-api</artifactId> <version>2.3.1</version> </dependency>
接下來,創建一個HelloWorld的Webservice服務,提供一個sayHello方法:
@WebService public class HelloWorld { public String sayHello(String name) { return "Hello, " + name; } }
然後,使用JAX-WS的方式創建客戶端代碼:
public class HelloWorldClient { public static void main(String[] args) { try { URL url = new URL("http://localhost:8080/hello?wsdl"); QName qname = new QName("http://webservice.example.com/", "HelloWorldImplService"); Service service = Service.create(url, qname); HelloWorld hello = service.getPort(HelloWorld.class); String result = hello.sayHello("world"); System.out.println(result); } catch (Exception e) { e.printStackTrace(); } } }
其中,url是Webservice服務的地址和WSDL文件的路徑,qname是服務的名稱和命名空間,service創建一個服務實例,hello是服務的代理類。
通過以上代碼,我們就可以調用Webservice介面了。
二、使用CXF框架調用Webservice介面
CXF是一種支持JAX-WS和JAX-RS的開源Web服務框架,常用於Webservice介面的開發和調用。
首先需要在pom.xml文件中配置CXF依賴:
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.3.3</version> </dependency>
接下來,創建一個HelloWorld的Webservice服務,提供一個sayHello方法:
@WebService public class HelloWorld { public String sayHello(String name) { return "Hello, " + name; } }
然後,使用CXF的方式創建客戶端代碼:
public class HelloWorldClient { public static void main(String[] args) { JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.setServiceClass(HelloWorld.class); factory.setAddress("http://localhost:8080/hello"); HelloWorld hello = (HelloWorld) factory.create(); String result = hello.sayHello("world"); System.out.println(result); } }
其中,JaxWsProxyFactoryBean是CXF的一個工廠類,可以創建服務端和客戶端代碼。使用setServiceClass設置服務的介面類,使用setAddress設置Webservice服務的地址。
通過以上代碼,我們就可以使用CXF框架調用Webservice介面了。
三、使用Apache HttpClient庫調用Webservice介面
Apache HttpClient是一個開源的Java HTTP客戶端庫,可以支持HTTP協議的客戶端編程。
首先需要在pom.xml文件中配置HttpClient依賴:
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.10</version> </dependency>
然後,使用Apache HttpClient的方式調用Webservice介面:
public class HelloWorldClient { public static void main(String[] args) { try { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost("http://localhost:8080/hello"); StringEntity stringEntity = new StringEntity("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" + "<soap:Body>" + "<sayHello xmlns=\"http://webservice.example.com/\">" + "<arg0>world</arg0>" + "</sayHello>" + "</soap:Body>" + "</soap:Envelope>"); httpPost.setEntity(stringEntity); httpPost.setHeader("Content-type", "text/xml;charset=UTF-8"); CloseableHttpResponse response = httpclient.execute(httpPost); HttpEntity entity = response.getEntity(); String result = EntityUtils.toString(entity, "UTF-8"); System.out.println(result); EntityUtils.consume(entity); response.close(); httpclient.close(); } catch (Exception e) { e.printStackTrace(); } } }
其中,我們需要手動拼接SOAP協議的XML請求數據,並使用HttpPost發送請求。同時需要設置請求的Content-type為text/xml;charset=UTF-8格式。
通過以上代碼,我們就可以使用Apache HttpClient庫調用Webservice介面了。
原創文章,作者:AXBH,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/141734.html