Because it is also the first time to use such technology, I here through an example of some of their own experience, but also hope that we point out the bad place, we encourage each other.
WebService is mainly used to exchange data and integrated data, the main technology currently used is Tcp/ip,html,xml,.net,java
1. First create a WebService project to publish the WebService service
MyWebService is a class for publishing a service, Mywebservice.java:
1 PackageCom.webservice;2 3 ImportJavax.jws.WebMethod;4 ImportJavax.jws.WebService;5 ImportJavax.xml.ws.Endpoint;6 7 @WebService8 Public classMyWebService {9 //method One, for publishingTen Publicstring GetS (string name) { One return"Hello:" +name; A } - - //method Two, not for publishing the@WebMethod (exclude =true) - Publicstring getS2 (string name) { - return"Hello:" +name; - } + - Public Static voidMain (string[] args) { +Endpoint.publish ("Http://localhost/hello",NewMyWebService ()); A } at}
Note: annotation @webserivce is used on this class to indicate that this class will not be sent to a webservice
Note @webmethod (exclude = true) means that this method will not be published, and later will explain
The Endpoint.publish (ARG1,ARG2) method has two parameters: Arg1 represents the address you publish, and Arg2 represents the service's implementation.
2. Compiling Mywebservice.java class
3. View WSDL (enter in the browser address bar:Http://localhost/hello ( consistent with Wywebservice.java ))
As long as you can see the following information, it has been published successfully
So far, a service has been released.
Summarize:
A. Add annotations on classes that need to be published @webservice
B. Publish a service through the Endpoint.publish () method
TIPS: All non-static methods in a class can be published
Conditions that cannot be published: 1, static methods, and final modified methods
2. Add annotations on method @webmethod (exclude = True)
4. Client access to published services
The WSDL document that we just generated you may not understand, but that's OK, as long as Java can recognize it. You can generate a code tool for client calls based on a WSDL document.
The command we need to use is as follows:
-D: Generate class file
-S: Generate Java files
-P: Custom Package structure
Wsimport-s. -P com.myWebservice.client http://localhost/hello?wsdl
I generated A. class file and a. java file under the Com/mywebservice/client directory under the E disk
command to run:
Files generated after execution:
Copy all the Java files to your client's project, my client project is ready, and all the generated files are copied to the client project:
5. Create a test program:The MyClient code is as follows:
1 Packagewebservice_client;2 3 ImportCom.webservice.MyWebService;4 ImportCom.webservice.MyWebServiceService;5 6 Public classmyclient {7 Public Static voidMain (string[] args) {8Mywebserviceservice MSS =NewMywebserviceservice ();9MyWebService ms =Mss.getmywebserviceport ();TenString s = ms.gets ("Clplee"); One System.out.println (s); A } -}
6, the test results in the console output:
So far, a webservice example has been created successfully.
WebService example Explanation