Title: Java methods and code examples for calling WebService services
Abstract: This article introduces several methods for Java to call WebService services and provides specific code examples. Including using axis2 to generate client code, using JAX-WS to generate client code, using Apache CXF to generate client code, and using Spring Boot to integrate WebService services. Through these methods, Java can be easily implemented to call WebService services.
Text:
Axis2 is an open source WebService framework. By using axis2 to generate client code, calling can be simplified. WebService service process.
First, create a Java project in Eclipse and import the axis2 related jar package.
Next, create a package named "axis2Client" in the root directory of the project, and create a new class under the package named "Axis2Client".
The following is a sample code that uses axis2 to call the WebService service:
package axis2Client; import org.apache.axis2.AxisFault; import org.apache.axis2.Constants; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.client.ServiceClient; import org.apache.axis2.client.async.AsyncResult; import org.apache.axis2.client.async.Callback; import org.apache.axis2.client.async.CallbackHandler; import org.apache.axis2.client.async.InvocationCallback; import org.apache.axis2.client.async.Result; import org.apache.axis2.transport.http.HTTPConstants; public class Axis2Client { public static void main(String[] args) { try { // 创建ServiceClient对象 ServiceClient client = new ServiceClient(); // 设置服务地址 Options options = new Options(); options.setTo(new EndpointReference("http://localhost:8080/axis2/services/MyService")); // 设置超时时间(可选) options.setTimeOutInMilliSeconds(60000); // 设置请求SOAP头(可选) options.setProperty(HTTPConstants.CHUNKED, Constants.VALUE_FALSE); options.setProperty(HTTPConstants.REUSE_HTTP_CLIENT, Constants.VALUE_TRUE); // 设置认证信息(可选) options.setUserName("username"); options.setPassword("password"); // 将配置应用到ServiceClient对象 client.setOptions(options); // 调用WebService方法 Object[] result = client.invokeBlocking( new QName("http://service.namespace.com/", "operationName"), new Object[] { "parameter" }, new Class[] { String.class }); // 处理返回结果 String response = (String) result[0]; System.out.println(response); } catch (AxisFault e) { e.printStackTrace(); } } }
Java API for XML Web Services (JAX-WS) is a Java standard for developing Soap-based Web services. By using JAX-WS to generate client code, WebService services can be called more conveniently.
First, create a Java project in Eclipse and import the JAX-WS related jar package.
Next, create a package named "jaxwsClient" in the root directory of the project, and create a new class under the package named "JaxwsClient".
The following is a sample code that uses JAX-WS to call a WebService service:
package jaxwsClient; import javax.xml.namespace.QName; import javax.xml.ws.Service; import java.net.URL; public class JaxwsClient { public static void main(String[] args) { try { // 创建服务的URL对象 URL url = new URL("http://localhost:8080/MyService?wsdl"); // 创建服务对象 QName qname = new QName("http://service.namespace.com/", "MyService"); Service service = Service.create(url, qname); // 获取服务的端口对象 MyServicePortType port = service.getPort(MyServicePortType.class); // 调用WebService方法 String response = port.operationName("parameter"); // 处理返回结果 System.out.println(response); } catch (Exception e) { e.printStackTrace(); } } }
Apache CXF is a A framework for developing Web services that simplifies the process of calling WebService services by using Apache CXF to generate client code.
First, create a Java project in Eclipse and import the Apache CXF related jar package.
Next, create a package named "cxfClient" in the root directory of the project, and create a new class under the package named "CxfClient".
The following is a sample code using Apache CXF to call WebService service:
package cxfClient; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; public class CxfClient { public static void main(String[] args) { try { // 创建JaxWsProxyFactoryBean对象 JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); // 设置服务地址 factory.setAddress("http://localhost:8080/MyService"); // 设置服务接口 factory.setServiceClass(MyServicePortType.class); // 创建接口代理对象 MyServicePortType port = (MyServicePortType) factory.create(); // 调用WebService方法 String response = port.operationName("parameter"); // 处理返回结果 System.out.println(response); } catch (Exception e) { e.printStackTrace(); } } }
Spring Boot provides support for WebService The integration function of the service makes it easier to call the WebService by using Spring Boot to integrate the WebService service.
First, in the Spring Boot project, add support for WebService, create an interface named "MyService", and define the operation method of WebService.
@WebService public interface MyService { @WebMethod String operationName(String parameter); }
Then, in the Spring Boot project, create a class named "MyServiceImpl", implement the "MyService" interface, and implement the operation methods defined in the interface.
@WebService(serviceName = "MyService") public class MyServiceImpl implements MyService { @Override public String operationName(String parameter) { // 业务逻辑处理 return "response"; } }
Finally, in the Spring Boot project, perform relevant configurations, start the Spring Boot application, and call WebService.
@RestController public class MyController { private final MyService myService; public MyController(MyService myService) { this.myService = myService; } @GetMapping("/invokeWebService") public String invokeWebService() { String response = myService.operationName("parameter"); return response; } }
Summary:
This article introduces several methods of Java calling WebService services and provides specific code examples. These methods can be used to easily implement Java calls to WebService services.
The above is the detailed content of What are the methods for calling WebService services in Java?. For more information, please follow other related articles on the PHP Chinese website!