Quick Start for Axis2: http://axis.apache.org/axis2/java/core/docs/quickstartguide.html
1. Eclipse axis2 plug-in installation
The installation method of the plug-in is available on the Axis2 homepage; see http://axis.apache.org/axis2/java/core/tools/eclipse/wsdl2java-plugin.html#Installation
Axis2 download: http ://axis.apache.org/axis2/java/core/download.cgi
After decompressing these two compressed packages, place them in the plugins directory under the eclipse installation directory, and restart the eclipse server , File new ->other, you can see the following two options, indicating that the plug-in is installed successfully
Second, Axis2 server development
1, in tomcat Deploy axis2 in the medium (in order to publish the webservice service next)
Download axis2-1.7.6-war.zip and unzip it to tomcat webapps In the directory, start the tomcat server, you will find that there is an axis2 folder in the webapps directory, enter http://localhost:8080/axis2 in the browser, and you will find the corresponding webpage, indicating that you have succeeded
2, create a WebService to be published
2.1, new java project: AxisService
2. 2. Write the WebService that needs to be published, build the package samples.quickstart.service.pojo in the src directory, and the new class StockQuoteService is as follows
package samples.quickstart.service.pojo;import java.util.HashMap;import java.util.Map .Entry;public class StockQuoteService {private HashMapmap = new HashMap (); public double getPrice(String symbol) {Double price = (Double) map.get(symbol); if (price != null) {return price.doubleValue();} return 42.00;} public String list() {String result = "{"; for (Entry entr y: map.entrySet()) {result += entry.getKey() + ":" + entry.getValue() + ",";} result += "}"; return result;} public void update(String symbol , double price) { map.put(symbol, new Double(price)); } public String sayHello(String name) { return "hello" + name + "axis2"; }}
3, publish WebService
3.1, package the Service to be released, click New in Eclipse -> File -> Other -> Axis2 wizards -> Axis2 Services Archiver
p>
3. 2. Fill in the above picture, class File Location is The working directory corresponds to the bin folder of the project, and check Include .class files only, click next
3.3, Skip WSDL is selected by default, Click next
3.4, default, continue next
3.5, default, continue next
span>3.6. As shown in the figure above, select the correct Class Name, otherwise the load will not be class
3.7. As shown in the figure above, fill in the output file location under axis2\web-inf\services of the tomcat directory, click Finish, you can find: F:\apache-tomcat-6.0.45\webapps\axis2\WEB-INF\ Added StockQuoteService.aar under the services directory
3.8, test-released WebService
Open the http://localhost:8080/axis2/services/listServices page, you can see the service published by:
Click on the StockQuoteService link to view wsdl
webservice is released successfully.
Third, Axis2 client development
span>1, File-new select Axis2 Code Generator and click next
2, choose to generate java file from wsdl, click next
span>3, select the generated wsdl file
4, Codegen option select default, click next
5, choose to save the generated java code to a specific project in the workspace
Generated client code As follows
< /span>
add the required jar package of axis2 to the project in. Otherwise, an error will be reported. The required jar is obtained from the lib directory in the axis2-1.7.6-bin.zip download package.
Create a new package test, then new class StockQuoteServiceTest, the code is as follows :
package test;import java.rmi.RemoteException ;import samples.quickstart.service.pojo.GetPrice;import samples.quickstart.service.pojo.GetPriceResponse;import samples.quickstart.service.pojo.ListResponse;import samples.quickstart.service.pojo.SayHello;import samples.quickstart. service.pojo.StockQuoteServiceStub;import samples.quickstart.service.pojo.Update;public class StockQuoteServiceTest {public static void main(java.lang.String args[]){ try{ StockQuoteServiceStub stub = new StockQuoteServiceStub ("http://localhost :8080/axis2/services/StockQuoteService?wsdl"); getPrice(stub); update(stub); list(stub); getPrice(stub); sayHello(stub);} catch(Exception e){ ep rintStackTrace(); System.err.println("\n\n\n");}} /* fire and forget */ public static void update(StockQuoteServiceStub stub){ try{ Update update = new Update(); update. setSymbol("ABC"); update.setPrice(43.35); stub.update(update); update.setSymbol("CDE"); update.setPrice(12.00); stub.update(update); System.err.println( "price updated");} catch(Exception e){ e.printStackTrace(); System.err.println("\n\n\n");}} /* two way call/receive */ public static void getPrice (StockQuoteServiceStub stub){ try{ GetPrice price = new GetPrice(); price.setSymbol("ABC"); GetPriceResponse response = stub.getPrice(price); System.err.println(response.get_return());} catch( Exception e){ e.printStackTrace(); System.err.println("\n\n\n");} } public static void list(StockQuoteServiceStub stub){ try {ListResponse listResponse = stub.list(); System.out.println(listResponse.get_return());} catch (RemoteException e) {// TODO Auto-generated catch block e .printStackTrace();}} public static void sayHello(StockQuoteServiceStub stub){ SayHello sayHello = new SayHello(); sayHello.setName("weir"); try {System.out.println(stub.sayHello(sayHello).get_return( ));} catch (RemoteException e) {// TODO Auto-generated catch block e.printStackTrace();}} }
The results of running the code are as follows:
The update method is invalid and the price is not set in. This problem has to be investigated material.
I wrote about it first to implement webservice using the Axis2 framework.