Webservice, based on axis best practices.

Source: Internet
Author: User
Tags xmlns

Axis principle Discussion and practical walkthrough

Axis is the framework of the Java platform that supports WebService, and the Xfire is the same. But it seems to be a lot more troublesome to use than Xfire.

It has three ways of supporting WebService:

1. Dynamic invocation Interface (DII)

2. Dynamic Proxy,

3. Stubs,

Three ways to introduce:

1. DII is the simplest way to write a Java file such as MyService, remember that you do not need to compile a class file, because axis will help you compile, you need to copy this Java file into a directory, we are copying Myservic.java to axis _example/jws directory, change the file name to Myservice.jws, and then write a client program that accesses it directly,

Import Org.apache.axis.client.Call;

Import Org.apache.axis.client.Service;

Import Javax.xml.namespace.QName;

Import Javax.xml.rpc.ServiceFactory;

Import Java.net.URL;

public class Client {

public static void main(String [] args) throws Exception {

Indicate the URL where the service resides

String Endpoint = "Http://localhost:8080/axis_example/jws/MyService.jws";

Create a service invocation (call)

Service service = new service ();

Call Call Service.createcall ();//Create Call object via service

Set the URL where the service resides

Call.settargetendpointaddress (new Java.net.URL (endpoint));

Method Name (Processservice) is consistent with Myservice.java method name

Call.setoperationname ("Processservice");

The Object array encapsulates the parameter, with the parameter "This is test!", calling Processservice (String Arg)

string ret = (string) call.invoke (new object[]{"This is test!"});

SYSTEM.OUT.PRINTLN (ret);

}

}

Axis_example works into Tomcat/webapps and launches Tomcat.

Compile Client.java, run one of the main methods to test, you can see the screen print out: "This is test!", you can see the Axis_example/web-inf directory jwsclasses The/jws/myservice.class file--axis automatically compiles the *.jws file from the endpointthat you visit and places it in the generated jwsclasses directory.

Note 1: in the above new object[]{"This is test!"} statement, only one parameter is passed. If the Myservic.java

Processservice (String Arg) is rewritten as

Processservice (String arg,string arg2)

You can pass multiple arguments through new object[]{"test", "Test2"}.

NOTE 2: The following warning appears on the console after you start Tomcat:

-Unable to find required classes (Javax.activation.DataHandler and javax.mail.i nternet. Mimemultipart). Attachment support is disabled.

This is because Activation.jar and Mail.jar are missing (this warning can be ignored by the instances in this article).

Activation.jar (current version 1.1) Download address

Http://java.sun.com/products/javabeans/jaf/downloads/index.html

Mail.jar (current version 1.4) Download address

http://java.sun.com/products/javamail/downloads/

2. Dynamic Proxy

Create a new Myserviceinterface.java file under Axis_example/src with the following:

Import Java.rmi.Remote;

Import java.rmi.RemoteException;

Public interface Myserviceinterface extends Remote {

public string Processservice (String arg) throws RemoteException;

}
Modify the Myservic.java file under AXIS_EXAMPLE/SRC to declare the class

public class MyService

Switch

public class MyService implements Myserviceinterface

Without compiling, copy the Myservic.java to the Axis_example/jws directory and change the file name to Myservice.jws.



Client development, change the main method in Axis_example/src/client.java, as follows:

public static void main(String [] args) throws Exception {

String wsdlurl = "http://localhost:8080/axis_example/jws/MyService.jws?wsdl";

String NamespaceURI = "Http://localhost:8080/axis_example/jws/MyService.jws";

String serviceName = "Myserviceservice";

Servicefactory servicefactory = Servicefactory.newinstance ();

Javax.xml.rpc.Service Service = Servicefactory.createservice (new URL (Wsdlurl), New QName (NamespaceURI, serviceName));

Myserviceinterface proxy = (myserviceinterface)

Service.getport (New QName (NamespaceURI, PortName), Myserviceinterface.class);

System.out.println ("This is" + proxy.processservice ("Dynamic proxy test!"));

}

Put the AXIS_EXAMPLE/SRC project under the Tomcat/webapps and start Tomcat.

Compile the client, run the main method, and you will see the results of the program execution.

The stub way ( recommended way ).

Create a new Myservic.java file under Axis_example/src with the following:

public class MyService {

public string Processservice (string arg) {

return arg;

}

}

Compiling Myservic.java

In the new DEPLOY.WSDD (can refer to the axis-bin-1_4.zip/axis-1_4/samples in the DEPLOY.WSDD----as long as guaranteed under the Classpath) file, the content is:

<deployment xmlns= "http://xml.apache.org/axis/wsdd/"

Xmlns:java= "Http://xml.apache.org/axis/wsdd/providers/java" >

<service name= "MyService" provider= "Java:rpc" >

<parameter name= "ClassName" value= "Com.cjl.test.MyService"/>

<parameter name= "Allowedmethods" value= "Processservice"/>

</service>

</deployment>

Start Tomcat

In this way, you must first ensure that the jar required by axis is classpath in your environment. I was using to put all the Jar,classpath = d:/ecustoms/j2sdk1.4.2_07/lib;d:/alex/workspace/mywebservice/webroot/web-inf/lib/ activation.jar;d:/alex/workspace/mywebservice/webroot/web-inf/lib/axis.jar;axis-ant.jar;d:/alex/workspace/ mywebservice/webroot/web-inf/lib/commons-discovery-0.2.jar;d:/alex/workspace/mywebservice/webroot/web-inf/lib/ jaxrpc.jar;d:/alex/workspace/mywebservice/webroot/web-inf/lib/saaj.jar;d:/alex/workspace/mywebservice/webroot/ web-inf/lib/tools.jar;d:/alex/workspace/mywebservice/webroot/web-inf/lib/wsdl4j-1.5.1.jar;d:/alex/workspace/ Mywebservice/webroot/web-inf/lib/commons-logging-1.0.4.jar

Execute in the Axis_example/web-inf directory:

Java-djava.ext.dirs=lib org.apache.axis.client.adminclient-lhttp://localhost:8080/axis_example/servlet/ Axisservlet DEPLOY.WSDD

After execution, you can see that the server-config.wsdd file is generated in the Axis_example/web-inf directory.

From the file name can see the approximate meaning, is a configuration file, this file is a WebService project core.

Start Tomcat,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.