WebService, like RMI in Java core technology, is used to enable the interaction of data between applications on heterogeneous platforms, except that the technology masks differences between languages. This is also the reason for its great way.
There are a variety of technologies to implement webservice that enable WebService using Jax-WS, CXF, Axis2, or metro, and then show you how to implement webservice in different ways. This blog shows you how to implement WebService using Jax-WS:
First, create the WebService server side:
1. Create a new javaproject named "Server";
2, create the Icomputeservice interface, code such as the following:
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 Icomputeservice 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 the class StartServer that starts the server-side service, such as the following code:
Package Com.ghj.server;import javax.xml.ws.endpoint;import com.ghj.service.impl.computeservice;/** * Start server-side Services * * @author Gaohuanjie */public class StartServer {public static void main (string[] args) {String address = "Http://localhos T:8888/compute "; Endpoint.publish (Address, New Computeservice ());}}
Second, create the WebService client:
1. Create a new javaproject named "Client";
2. Copy the Icomputeservice interface in the "Server" project to "client" project;
3. Create code that invokes the "server" Projecticomputeservice interface implementation class. 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 an access to the WSDL service address urlqname sname = new QName ("http:// impl.service.ghj.com/"," Computeserviceservice ");//Specify the details of the service through QName, with the reference to the 1.png Chart Service service in this project = Service.create (url,sname);//Create service Icomputeservice Computeservice = Service.getport (Icomputeservice.class);// Implement Interface System.out.println (Computeservice.add (12,33));} catch (Malformedurlexception e) {e.printstacktrace ();}}}
Third, the implementation of WebService:
1. Execute the StartServer class in "Server" project;
2. Execute the client class in "Client" project.
"0-minute download resources "
WEBSERVICE:JAX-WS Implementation WebService