What is WebService? In my understanding, a method in a class is published as a Web Service for others to call (a method is a service ). The most common web services include weather forecasts and mobile phone numbers.
More professional theoretical knowledge, Google, you will know ~
There are many ways to develop Web services, such as Apache axis2, Apache cxf, xfire, and java6.
A new feature of Java 6 is to publish a class into a WebService through a simple annotaion.
This article describes how to use Java to write, publish, and call your own web services.
I. Write service code:
Package COM. xcy; import javax. JWS. webService;/*** @ author Xiao chunyong (siuon) * @ version 1.0 * @ create 2012-7-19 7:28:12 * // Add the annotation @ WebService @ webservicepublic class java6wb on the class to be published as WebService * @ return */Public String sayhello (string name) {return "hello" + name;}/*** provides an addition Service * @ Param A * @ Param B * @ return */Public int add (int, int B) {return a + B ;}}
Ii. Publish services
Package COM. xcy; import javax. XML. WS. endpoint;/*** @ author Xiao chunyong (siuon) * @ version 1.0 * @ create 2012-7-19 7:33:27 */public class test {public static void main (string [] ARGs) {endpoint. publish ("http: // localhost: 8080/WS/java6wb", new java6wb ());}}
After running, open the browser to access: http: // localhost: 8080/WS/java6wb? WSDL:
3. Use JDK 6 to generate client code:
Java6 A wsimp.exe .exe program is used to parse the WSDL file and generate client code. (Wsimport.exe In the JDK \ bin directory );
Assume that you have configured Java environment variables.
Wsimport-d [class file storage directory]-s [source code storage directory]-P [package name]-Keep [wsdl uri]
Here, I only specify the package name and the WSDL URI. In this case, the class file and source code will be in the directory indicated by the package (in fact, you can direct the source code directory to your project)
4. Call the Web Service through the generated client code
Package test; import COM. xcy. webService. client. java6wb; import COM. xcy. webService. client. java6wbservice;/*** @ author Xiao chunyong (siuon) * @ version 1.0 * @ create 2012-7-19 8:05:40 */public class test {public static void main (string [] ARGs) {// create the client object java6wb java6wb = new java6wbservice (). getjava6wbport (); // call String result = java6wb. sayhello ("siuon"); system. out. println (result); int sum = java6wb. add (1, 3); system. out. println (SUM );}}
Result
Apache axis2 + eclipse WebService Development