Introduction to Apache CXF: http://cxf.apache.org/docs /index.html
1. Use CXF to develop Web Service server:
4.0.0 com.webService cxfWebService 0.0.1-SNAPSHOT jar cxfWebService http://maven.apache.org UTF-8 2.2.3 org.apache.cxf cxf-rt-frontend-jaxws ${cxf.version} org.apache.cxf cxf-rt-transports-http ${cxf.version} org.apache.cxf cxf-rt-transports-http-jetty ${cxf.version}
2, Develop a webservice business interface, the method is decorated with @WebService.
@WebService public interface HelloWorld {String sayHi(String name); }< /pre>
package com.weir;import java.util.Date;import javax.jws.WebService ;@WebService(endpointInterface="com.weir.HelloWorld",serviceName="HelloWorldWs")//Specify the interface and service name implemented by the webservice public class HelloWorldImpl implements HelloWorld{ public String sayHi(String name) {// TODO Auto- generated method stub return name+"Hello! The time now is:"+new Date(); }}
4. Expose the Web Service function, run the function to expose the Web Service:
package com.weir;import javax.xml.ws.Endpoint;public class ServiceMain {public static void main(String args[]){ HelloWorld hw = new HelloWorldImpl(); // Call the publish method of Endpoint to publish Web Service String address="http://localhost :8080/helloWorldService"; Endpoint.publish(address, hw); System.out.println("Web Service exposed successfully!"); }}
span>Open the browser and enter http://localhost:8080/helloW orldService?wsdl, you can see the wsdl description file when the service is successfully exposed
two Develop the Web Service client:
2, use eclipse new Web Service Client generates client code
package com.weir.test;import java.rmi.Remot eException;import com.weir.HelloWorldProxy;public class TestService {public static void main(String args[]){ HelloWorldProxy helloWorldProxy = new HelloWorldProxy(); try {String s = helloWorldProxy.sayHi("weir"); System.out. println("Call webservice:"+s);} catch (RemoteException e) {// TODO Auto-generated catch block e.printStackTrace();} }}
Run the main function and successfully call WebService
Each Web Service component requires 2 parts: interface And implementation class:
1, create a new maven project cxfWebService
Project directory structure:
This is the content of the pom.xml file
4.0.0 com.webService cxfWebService 0.0.1-SNAPSHOT jar cxfWebService http://maven.apache.org UTF-8 2.2.3 org.apache.cxf cxf-rt-frontend-jaxws ${ cxf.version} org.apache.cxf cxf-rt-transports-http ${cxf.version} org.apache.cxf cxf-rt-transports-http-jetty ${cxf. version}
2, develop a webservice business interface, use @WebService Retouch.
@WebService public interface HelloWorld {String sayHi(String name); }< /pre>
package com.weir;import java.util.Date;import javax.jws.WebService ;@WebService(endpointInterface="com.weir.HelloWorld",serviceName="HelloWorldWs")//Specify the interface and service name implemented by the webservice public class HelloWorldImpl implements HelloWorld{ public String sayHi(String name) {// TODO Auto- generated method stub return name+"Hello! The time now is:"+new Date(); }}
4. Expose the Web Service function, run the function to expose the Web Service:
package com.weir;import javax.xml.ws.Endpoint;public class ServiceMain {public static void main(String args[]){ HelloWorld hw = new HelloWorldImpl(); // Call the publish method of Endpoint to publish Web Service String address="http://localhost :8080/helloWorldService"; Endpoint.publish(address, hw); System.out.println("Web Service exposed successfully!"); }}
span>Open the browser and enter http://localhost:8080/helloWorldSe rvice?wsdl, you can see the wsdl description file when the service is successfully exposed
@WebService public interface HelloWorld {String sayHi(String name); }
p>
Two, develop Web Service client:
2. Use eclipse's new Web Service Client to generate client code
1. Create a new java project cxfWebServiceClient
Select the wsdl address just now
< /p>
select the location of the generated code, here we select the newly created project cxfWebServiceClient
Yes can see that there are more files under our project
3, write test code on the client side
package com.weir.test;import java.rmi.RemoteException;import com.weir.HelloWorldProxy;public class TestService {public static void main(String args[]){ HelloWorldProxy helloWorldProxy = new HelloWorldProxy(); try {String s = helloWorldProxy.sayHi("weir"); System.out.println("Call webservice:"+s);} catch (RemoteException e) {// TODO Auto-generated catch block e.printStackTrace();} }}
Run the main function and successfully call WebService