Axis2 Create a WebService instance

第一步:新建Web Project,项目名称为webservcie
      

第二步:新建一个简单的打印字符串的Class HelloWorld.java:

package demo;public class HelloWorld {public String printStr(String name) {String resultStr = "Hello," + name; System.out.println(resultStr); return resultStr; }}

Step 3:Right-click HelloWorld.java—Web Services—Create Web service. Click Next…finish in turn, and find that a wsdl directory is generated under the WebContent directory, and there is a HelloWorld.wsdl under the directory.

< p>

Step 4: Test web service: Right-click HelloWorld.wsdl—Web Services—Test with Web Service s Explorer—click the method name printStr —-input parameter Alexia—click go. At this time, you will find the console output Hello, Alexia, indicating that the test is successful, and there is no problem with WebService

Step 5: Write the client calling class HelloWorldTest, and use AXIS2 to remotely call HelloWorld (in order to reflect the remote call, you can create a new Project), the code is as follows:

package demo;import java.rmi.RemoteException;import javax.xml.rpc.ParameterMode ;import javax.xml.rpc.ServiceException;import org.apache.axis.client.Call;import org.apache.axis.client.Service;import org.apache.axis.encoding.XMLType;//http://localhost :8899/webservice/services/HelloWorld?wsdlpublic class HelloWorldTest {public String invokeRemoteFuc() {// Remote call path String endpoint = "http://localhost:8899/webservice/services/HelloWorld"; String result = "call failed! "; Service service = new Service(); Call call; try {call = (Call) service.createCall(); call.setTargetEndpointAddress(endp oint); // Called method name call.setOperationName("printStr"); // Set parameter name call.addParameter("name", // Parameter name XMLType.XSD_STRING, // Parameter type: String ParameterMode.IN); // Parameter mode:'IN' or'OUT' // Set the return value type call.setReturnType(XMLType.XSD_STRING); // Return value type: String String name = "aaaa"; result = (String) call.invoke( new Object[] {name });// remote call} catch (ServiceException e) {e.printStackTrace();} catch (RemoteException e) {e.printStackTrace();} return result;} // test public static void main(String[] args) {         HelloWorldTest test = new HelloWorldTest();         String result = test.invokeRemoteFuc();         System.out.println(result);     } }

< /p>

The sixth step:Test: HelloWorldTest —Run as—Java application, if it is found that the console outputs Hello aaaa correctly, the test is successful

Leave a Comment

Your email address will not be published.