一、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/n/141734.html
微信扫一扫
支付宝扫一扫