WebService: JAX-WS implementation WebService
Like RMI in core Java technologies, WebService is used to implement data interaction between applications on heterogeneous platforms. The only difference is that this technology shields the differences between languages, this is also the reason for its popularity. WebService is implemented in a variety of technologies. You can use JAX-WS, CXF, Axis2, or Metro to implement WebService. Next, we will show you how to implement WebService in different ways, this blog shows you how to use JAX-WS to implement WebService:
1. Create a WebService Server:
1. Create a Java project named "server;
2. Create the IComputeService interface. The Code is as follows:
Package com. ghj. service; import javax. jws. webService; import javax. jws. soap. SOAPBinding;/*** SEI Service Endpoint Interface published Service Interface ** @ author GaoHuanjie */@ WebService @ SOAPBinding (style = SOAPBinding. style. RPC) public interface IComputeService {public int add (int a, int B );}
3. Create ComputeService interface implementation class ComputeService. The Code is as follows:
Package com. ghj. service. impl; import javax. jws. webService; import com. ghj. service. IComputeService;/*** SIB Service Implemention Bean ** @ author GaoHuanjie * // endpointInterface specifies the Access Point interface: The interface must exist @ WebService (endpointInterface = "com. ghj. service. IComputeService ") public class ComputeService implements IComputeService {@ Overridepublic int add (int a, int B) {System. out. println (a + "+" + B + "=" + (a + B); return a + B ;}}
4. Create a StartServer class to start the Server Service. The Code is as follows:
Package com. ghj. server; import javax. xml. ws. endpoint; import com. ghj. service. impl. computeService;/*** start server services ** @ author GaoHuanjie */public class StartServer {public static void main (String [] args) {String address = "http: // localhost: 8888/compute "; Endpoint. publish (address, new ComputeService ());}}
2. Create a WebService client:
1. Create a Java project named "client;
2. Copy the IComputeService interface in the "server" project to the "client" project;
3. Create a code that calls the IComputeService interface implementation class of the "server" project. The Code is as follows:
Package com. ghj. client; import java.net. malformedURLException; import java.net. URL; import javax. xml. namespace. QName; import javax. xml. ws. service; import com. ghj. service. IComputeService; public class Client {public static void main (String [] args) {try {URL url = new URL ("http: // localhost: 8888/compute? Wsdl "); // create urlQName sname = new QName (" http://impl.service.ghj.com/"," ComputeServiceService ") to access the wsdl Service address; // use qnameto indicate the specific information of the service, and its value corresponds to the 1.png graph Service = Service in the project. create (url, sname); // create service IComputeService computeService = service. getPort (IComputeService. class); // implement the interface System. out. println (computeService. add (12,33);} catch (MalformedURLException e) {e. printStackTrace ();}}}
3. Run WebService:
1. Run the StartServer class in the "server" project;
2. Run the client class in the "Client" project;