1, write WebService class, use @webservice annotation
Package test; Import Javax.jws.WebService; @WebService Public class helloserviceimpl{ public string Say (string name) { return "Hello" + name;} }
WebService Class
2, use the Main method to publish WebService
Package test; Import Javax.xml.ws.Endpoint; Public class Publisher { publicstaticvoid main (string[] args) { Endpoint.publish (new Helloserviceimpl ());} }
Main method Publish WebService
3, after the successful release, access to the publishing address +?WSDL get the Network Service Description language, where the TNS colon within the node represents targetnamespace, point to the reference node
This XML file does does appear to has any style information associated with it. The document tree is shown below.<!--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://test/"xmlns:xsd= "Http://www.w3.org/2001/XMLSchema"xmlns= "http://schemas.xmlsoap.org/wsdl/"targetnamespace= "http://test/"name= "Helloserviceimplservice"><Types><Xsd:schema><Xsd:importnamespace= "http://test/"schemalocation= "Http://192.168.0.103:8088/hello?xsd=1"/></Xsd:schema></Types><messagename= "Say">< Partname= "Parameters"element= "Tns:say"/></message><messagename= "Sayresponse">< Partname= "Parameters"element= "Tns:sayresponse"/></message><PortTypename= "Helloserviceimpl"><Operationname= "Say"><inputwsam:action= "Http://test/HelloServiceImpl/sayRequest"message= "Tns:say"/><Outputwsam:action= "Http://test/HelloServiceImpl/sayResponse"message= "Tns:sayresponse"/></Operation></PortType><bindingname= "Helloserviceimplportbinding"type= "Tns:helloserviceimpl"><soap:bindingTransport= "Http://schemas.xmlsoap.org/soap/http"style= "Document"/><Operationname= "Say"><soap:operationSOAPAction=""/><input><Soap:body Use= "literal"/></input><Output><Soap:body Use= "literal"/></Output></Operation></binding><Servicename= "Helloserviceimplservice"><Portname= "Helloserviceimplport"binding= "Tns:helloserviceimplportbinding"><soap:address Location= "Http://192.168.0.103:8088/hello"/></Port></Service></Definitions>
WSDL
4. Build WebService client Java class using Wsimport in JDK
Wsimport-s. -P com.hjp.stub Http://192.168.0.103:8088/hello?wsdl-Xnocompile
-S followed by a dot in the current directory, the first parameter after-p to represent the package that generated the class, the second parameter is the wsdl,-xnocompile representation of the WebService service does not need to compile, if removed-xnocompile will have compiled class file
5, the fourth step to generate a good Java file, copied to the client project, write the client test code
Packagecom.hjp.client;ImportCom.hjp.stub.HelloServiceImpl;ImportCom.hjp.stub.HelloServiceImplService; Public classClient { Public Static voidMain (string[] args) {//Create a service access Point collection ObjectHelloserviceimplservice helloserviceimplservice=NewHelloserviceimplservice (); //classes that get the service point bindingsHelloserviceimpl helloservice=Helloserviceimplservice.gethelloserviceimplport (); //invoking a service-side methodString Returnstr=helloservice.say ("Xiaoming"); System.out.println (RETURNSTR); }}
Client Code
6. Extended WebService parameters
If you want to modify the name of the node within the WSDL, you can set @webservice (on the Class), @WebMethod (on the method), @WebParam (before the parameter) in the name parameter
If you exclude one of these methods, use @webmethod inside exclude=true
Wsimport command in WebService's JDK