Detailed explanation of the method of calling WebService in Java
Overview:
With the development of the Internet, Web services have become an indispensable part. Web service is a distributed computing model based on the HTTP protocol. It provides a standardized interface through the network so that applications on different platforms can communicate with each other and exchange data. As a widely used programming language, Java provides a wealth of libraries and tools to facilitate developers to call WebService.
This article will introduce in detail the method of calling WebService in Java and give code examples to help developers better understand and apply it.
1. The basic concept of WebService
WebService is a software system that can be accessed through the network. It uses standardized XML format for data transmission, and generally uses the SOAP protocol (Simple Object Access Protocol) as communication protocol. WebService usually has the following characteristics:
2. Method of calling WebService in Java
In Java, we can use Java's own WebService related libraries and tools to call WebService. The following methods are commonly used:
import javax.jws.WebService; @WebService public class HelloWorld { public String sayHello(String name) { return "Hello, " + name + "!"; } }
In this example, we define a simple WebService that provides a method named sayHello to return a greeting. Using JAX-WS, we can call this WebService through the following code:
import javax.xml.namespace.QName; import javax.xml.ws.Service; import java.net.URL; public class HelloWorldClient { public static void main(String[] args) { try { URL url = new URL("http://localhost:8080/HelloWorld?wsdl"); QName qname = new QName("http://webservice.example.com/", "HelloWorldService"); Service service = Service.create(url, qname); HelloWorld hello = service.getPort(HelloWorld.class); System.out.println(hello.sayHello("John")); } catch (Exception e) { e.printStackTrace(); } } }
In this client code, we first create a URL object that points to the WSDL address of the WebService we want to call. Then, we created a Service object using the URL, and obtained the WebService interface we want to call through the Service object. Finally, we called the sayHello method of the WebService interface and printed the returned result.
import org.apache.axis2.AxisFault; import org.apache.axis2.Constants; import org.apache.axis2.client.Options; import org.apache.axis2.client.ServiceClient; import org.apache.axis2.rpc.client.RPCServiceClient; import org.apache.axis2.wsdl.WSDLConstants; public class HelloWorldClient { public static void main(String[] args) { try { RPCServiceClient rpcServiceClient = new RPCServiceClient(); Options options = rpcServiceClient.getOptions(); options.setProperty(Constants.Configuration.DISABLE_SOAP_ACTION, true); options.setProperty(WSDLConstants.WSDL_LOCATION, "http://localhost:8080/HelloWorld?wsdl"); options.setTimeOutInMilliSeconds(10000); QName qname = new QName("http://webservice.example.com/", "HelloWorldService"); String method = "sayHello"; Object[] parameters = new Object[] { "John" }; Class>[] returnTypes = new Class[] { String.class }; Object[] response = rpcServiceClient.invokeBlocking(qname, method, parameters, returnTypes); String result = (String) response[0]; System.out.println(result); } catch (Exception e) { e.printStackTrace(); } } }
In this client code, we first create an RPCServiceClient object. Then, we set the WSDL address and timeout of the WebService. Next, we define the method name (sayHello) and parameters (John) to be called, and call the method through the RPCServiceClient object. Finally, we print the returned results.
The above are two common methods for calling WebService in Java. Developers can choose the appropriate method to call WebService based on specific needs and usage scenarios.
Conclusion:
This article introduces the method of calling WebService in Java and provides relevant code examples. Through learning and practice, developers can better understand and apply WebService and improve development efficiency and code quality.
The above is the detailed content of In-depth analysis of the method of calling WebService in Java. For more information, please follow other related articles on the PHP Chinese website!