There are many ways to implement a WEB service, this article describes the implementation of JAX-WS (Pure Java), and then generates JavaBean from the CXF tool.
The first step: Create a Java project, write the Calservice interface, which is the interface that provides the Web service.
1 PackageCom.fx.inf;2 3 ImportJavax.jws.WebMethod;4 ImportJavax.jws.WebService;5 6 //Mark Service Endpoint Interface (SEI)7 @WebService8 Public InterfaceCalservice {9 Ten //methods applied to the Server service endpoint interface (SEI)-summation method One @WebMethod A Public DoubleAddDoubleIDoublej); - - //methods applied to the Server service endpoint interface (SEI)-the method of finding the difference the @WebMethod - Public DoubleSubDoubleIDoublej); - - //methods applied to the Server service endpoint interface (SEI)-the method of quadrature + @WebMethod - Public DoubleMulDoubleIDoublej); + A //methods applied to the Server service endpoint interface (SEI)-the method of seeking quotient at @WebMethod - Public DoubleDivDoubleIDoublej); - -}
Step Two: Write the implementation class Calserviceimpl of the interface.
1 PackageCom.fx.inf.impl;2 3 ImportJavax.jws.WebMethod;4 ImportJavax.jws.WebService;5 6 ImportCom.fx.inf.CalService;7 8 //Endpointinterface indicates that the class must implement all methods of this interface, servicename represents the service name of the Web service9@WebService (endpointinterface= "Com.fx.inf.CalService", servicename= "Calservice")Ten Public classCalserviceimplImplementsCalservice { One A @Override - @WebMethod - Public DoubleAddDoubleIDoublej) { the //TODO auto-generated Method Stub - returni +J; - } - + @Override - @WebMethod + Public DoubleSubDoubleIDoublej) { A //TODO auto-generated Method Stub at returnIJ; - } - - @Override - @WebMethod - Public DoubleMulDoubleIDoublej) { in //TODO auto-generated Method Stub - returnIJ; to } + - @Override the @WebMethod * Public DoubleDivDoubleIDoublej) { $ //TODO auto-generated Method StubPanax Notoginseng if(J! = 0 ) { - returnIJ; the}Else{ + return0; A } the } + -}
Step three: Write the test method and publish the service.
1 Packagecom.fx.test;2 3 ImportJavax.xml.ws.Endpoint;4 5 ImportCom.fx.inf.CalService;6 ImportCom.fx.inf.impl.CalServiceImpl;7 8 Public classCalservicetest {9 Ten Public Static voidMain (string[] args) { OneSYSTEM.OUT.PRINTLN ("--->web Service Start"); A -Calservice cal =NewCalserviceimpl (); -String address = "Http://localhost:8888/cal"; the //Publish this service - endpoint.publish (address, Cal); - } - +}
Run the test and publish successfully:
Fourth step: Open a browser, enter http://localhost:8888/cal? WSDL, get some information, this is the WSDL file:
Borrow the CXF tool (there are instructions in the bin directory of CXF to automatically convert the WSDL file to a JavaBean file) and compile the WSDL into a JavaBean file:
Fifth step: Copy the resulting files to a new Java project:
To write a test class:
1 Packagecom.fx.test;2 3 ImportCom.fx.inf.CalService;4 5 Public classCaltest {6 7 Public Static voidMain (string[] args) {8 9Com.fx.inf.impl.CalService cal =NewCom.fx.inf.impl.CalService ();Ten OneCalservice Calser =Cal.getcalserviceimplport (); A -SYSTEM.OUT.PRINTLN ("--->add:" + calser.add (1, 2)); -SYSTEM.OUT.PRINTLN ("--->sub:" + calser.sub (1, 2)); theSYSTEM.OUT.PRINTLN ("--->mul:" + Calser.mul (1, 2)); -SYSTEM.OUT.PRINTLN ("--->div:" + calser.div (1, 2)); - - } + -}
Run and get results:
PS: Here must note that the previous release of the service of the Java project do not stop, otherwise this will be an error:
JAX-WS and CXF implementation of Web Service