First, create a webservice:
PackageGarfield;ImportJavax.jws.WebService;Importjavax.xml.ws.Endpoint; @WebService Public classMyj6webservice { Publicstring SayHello (String strName) {return"Hello," +strname+ "!"; } Public Static voidMain (string[] args) {//release WebService, note If prompted: Exception in Thread "main" Com.sun.xml.internal.ws.server.ServerRtException:Server Runtime Error:java.net.BindException:Address already in Use:bind//you need to modify the publish portEndpoint.publish ("Http://localhost:8030/garfield.MyJ6WebService",NewMyj6webservice ()); System.out.println ("WebService was published success!"); }}
Start Tomcat and run, and the system will output: WebService was published success!, which means WebService is started.
You can enter the address in the browser: http://localhost:8030/garfield.MyJ6WebService?wsdl
Show WebService Information:
<?XML version= "1.0" encoding= "UTF-8"?> - <!--Published by Jax-ws RI at http://jax-ws.dev.java.net. Ri ' s version is JAX-ws RI 2.2.4-B01. - - <!--Generated by Jax-ws RI at http://jax-ws.dev.java.net. Ri ' s version is JAX-ws RI 2.2.4-B01. - - <DefinitionsXMLNS:WSU= "Http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"xmlns:wsp= "Http://www.w3.org/ns/ws-policy"xmlns:wsp1_2= "Http://schemas.xmlsoap.org/ws/2004/09/policy"Xmlns:wsam= "Http://www.w3.org/2007/05/addressing/metadata"Xmlns:soap= "http://schemas.xmlsoap.org/wsdl/soap/"Xmlns:tns= "http://garfield/"xmlns:xsd= "Http://www.w3.org/2001/XMLSchema"xmlns= "http://schemas.xmlsoap.org/wsdl/"targetnamespace= "http://garfield/"name= "Myj6webserviceservice">- <Types>- <Xsd:schema> <Xsd:importnamespace= "http://garfield/"schemalocation= "Http://localhost:8030/garfield.MyJ6WebService?xsd=1" /> </Xsd:schema> </Types>- <messagename= "SayHello"> < Partname= "Parameters"element= "Tns:sayhello" /> </message>+ <messagename= "Sayhelloresponse"> < Partname= "Parameters"element= "Tns:sayhelloresponse" /> </message>- <PortTypename= "Myj6webservice">- <Operationname= "SayHello"> <inputwsam:action= "Http://garfield/MyJ6WebService/SayHelloRequest"message= "Tns:sayhello" /> <Outputwsam:action= "Http://garfield/MyJ6WebService/SayHelloResponse"message= "Tns:sayhelloresponse" /> </Operation> </PortType>- <bindingname= "Myj6webserviceportbinding"type= "Tns:myj6webservice"> <soap:bindingTransport= "Http://schemas.xmlsoap.org/soap/http"style= "Document" /> - <Operationname= "SayHello"> <soap:operationSOAPAction="" /> - <input> <Soap:body Use= "literal" /> </input>- <Output> <Soap:body Use= "literal" /> </Output> </Operation> </binding>- <Servicename= "Myj6webserviceservice">- <Portname= "Myj6webserviceport"binding= "Tns:myj6webserviceportbinding"> <soap:address Location= "Http://localhost:8030/garfield.MyJ6WebService" /> </Port> </Service> </Definitions>
In the WebService boot state, on the command line, enter:
E:\temp>wsimport-p garfield.garfieldj6wsclient-keep http://localhost:8030/garfield. Myj6webservice? wsdlparsing wsdl...generating code...compiling code ... E:\Temp>
Note that you must correspond to the WebService information you publish!
Then the system will automatically generate the relevant interface files, as follows:
Create a new Java project, introduce the output file, and then build the test class:
Packagexxh;Importgarfield.garfieldj6wsclient.*; Public classWebClient {/** * @paramargs*/ Public Static voidMain (string[] args) {//Create a Client service objectMyj6webservice Myj6ws =NewMyj6webserviceservice (). Getmyj6webserviceport (); //Call the service method and get the method return valueString strtest = Myj6ws.sayhello ("Garfield"); //The return value of the print serviceSystem.out.println (strtest); } }
Under WebService run, run the client test program, output:
Hello, garfield!.
Java WebService simple use case