Quick build AXIS app

xiaoxiao2021-03-18  224

This article is to use AXIS to quickly build a system structure that can be used for actual project development, and will not be described in the theory of SOAP protocol.

This article uses Jetty as a server, Jetty is a free open source 100% pure Java HTTP server and servlet container, which has the advantages of lightweight, high performance, and high reliability, embedded, scalable.

IDE uses Eclipse.

Service construction

Services AxisServer, in LIB, the JAR files used by the server, including Jetty related JAR files, and AXIS related JAR files, all of which are all imported into the project. Server-config.wsdd is the SOAP server configuration file, including SOAP service access portfolio, related parameters, and custom class. AxisServer.java is used to launch the Jetty server and register the AXIS service. AxisServerInterface.java is an interface to serve the client, and the client can access all open Web Services services through it. Player.java is an object you define, and you need to pass this object between the server and the client through AXIS. MyException is a custom exception that encloses errors within the project.

AxisServer.java

Public class axisserver {

Public void start () {

Server Server = new server ();

SocketListener Listener = New SocketListener ();

Listener.Setport (8080);

ServlethttpContext Context = (servlethttpContext) Server

.getContext ("/");

Context.setResourceBase ("./ Web /");

Try {

Context.addServlet ("AxisDefault", "/ WebServices / *",

"org.apache.axis.transport.http.axisservlet");

Server.addContext (context);

Server.Addlistener (Listener);

Server.Start ();

} catch (exception e) {

E.PrintStackTrace ();

}

}

Public static void main (String [] args) {

New axisserver (). start ();

}

}

AxisServer.java is the project is the launch program, responsible for launching the Jetty server, registering the Web Services service.

AxisServerInterface.java

Public class axisserverinterface {

PUBLIC STRING PING () throws myexception {

RETURN "Do u Hear ME?";

}

Public Player getPlayer (int id) throws myexception {

IF (id <0) {

Throw New MyException ("Invalid Student");

}

Player s = new player ();

S.SetID (ID);

S.setname ("Bush" ID);

Return S;

}

}

This program is a server entry and now provides a ping and getstudent method. The client can access these two services via AXIS.

Player.java

Public class player {

Private int ID;

PRIVATE STRING NAME;

PRIVATE STRING TEAM;

Public int getId () {

Return ID;

}

Public void setid (int id) {

THIS.ID = ID;

}

Public string getname () {

Return Name;

}

Public void setname (String name) {

THIS.NAME = Name;

}

Public string getteam () {

Return team;

}

Public void setteam (string team) {

THIS.TEAM = Team;

}

}

Player is an object that saves data and is responsible for passing data between the server and the client.

MyException.java

Public class myexception extends Exception Implements Serializable {

String ErrorMsg;

Public myException () {

}

Public myException (String Message) {

ERRORMSG = Message;

}

Public string geterror () {

Return ErrorMsg;

}

Public void setError (string errormsg) {

THIS.ERRORMSG = ErrorMsg;

}

}

MyException is used to encapsulate an exception generated within the project, through it, you can pass an exception to the client to the client.

Server-config.wsdd

Qname = "ns: player"

XMLns: ns = "http://service.server.axis.mi.com"

LanguagespecificType = "java: com.mi.axis.server.service.player" />

Qname = "ns: myexception"

XMLns: ns = "http://service.server.axis.mi.com"

LanguagespecificType = "java: com.mi.axis.server.service.myexception" />

This file performs the definition of the AXIS server, defines the relevant properties of the web services provider, Defines the object to be passed.

Client build

AxisClient is a client project, which introduces all Axis related JAR files for the server project. Client.java is used to demonstrate how to access Web Services. Build.xml is to build a client program that generates CLIENT.JAVA to use Ant. Axis.wsdl is the generated intermediate file.

First execute build.xml compiling by Ant generation, and then the client program client.java invokes the client program access server.

Build.xml

WEB Services via the server-connected Web Services Axis.wsdl, generates a client program that generates asix.wsdl by , the program is generated in com.mi.axis.wsdl Package. Note that the project must introduce Axis-Ant.jar.

Client.java

This program accesses the Web Services provided by the server, get the relevant result, before performing this program, start the server first, and execute the AxisSrver.java program.

Public class client {

Public static void main (String [] args) {

AxisServerInterface asi = null;

Try {

URL URL = New URL ("http: // localhost: 8080 / webservices / asi");

AxisServerInterfaceServiceLocator Loc = New AxisServerInterFaceServiceLocator ();

Loc.setMainTainSession (TRUE);

ASI = Loc.getaxisServerInterfac (URL);

System.out.println (asi.ping ());

} catch (exception e) {

}

Try {

Player P = Asi.getPlayer (12);

System.out.println (p.getname ());

P = asi.getplayer (-12);

} catch (myexception e) {

System.out.println ("I've Got IT");

} catch (remoteException E) {

System.out.println ("strang");

}

}

}

When an asi.getplayer (-12) is executed, the Catch to the system-defined exception myexception.

carried out:

Start AxisServer first, then execute the client, get the Player object to be passed, and the MyException to be Catch.

Related Program URL:

Eclipse:

www.eclipse.org

Jetty: jetty.mortbay.orgaxis: www.apache.org

转载请注明原文地址:https://www.9cbs.com/read-129826.html

New Post(0)