Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
515 views
in Technique[技术] by (71.8m points)

java - Steps in creating a web service using Axis2 - The client code

I am trying to create a web service, my tools of trade are:

**

Axis2, Eclipse, Tomcat, Ant

**

I need to create a web service from Code, i.e. Write a basic java class which will have the methods to be declared in the WSDL. Then use java2WSDL.sh to create my WSDL.

So, is this approach correct:

  1. Write my Java class with actual business logic
package packageNamel;

public class Hello{
public void World(String name)
          {
            SOP("Hello" + name);
          }
}
  1. Now, when I pass this Hello.java to java2WSDL.sh, this will give me the WSDL.
  2. Finally, I will write the services.xml file, and create the Hello.aar with following dir structure:

    Hello.aar

    • packageName
      • Hello.class
    • META-INF
      • services.xml
      • MANIFEST.MF
      • Hello.WSDL

Now, I assume, my service will be deployed when I put the aar in tomcat1/webapps/axis2/WEB-INF/services

But, here comes my problem, HOW DO I ACCESS THE METHOD World(String name)???!!, i.e. I am clueless about the client code!

Please enlighten me on making a very basic web service and calling the method. The above described 3 steps might be wrong. It's a community wiki, feel free to edit.

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm assuming you're only interested in web service clients?

Option 1

Invoke the web service is using Axis2 REST support, for example:

http://localhost:8080/axis2/services/MyService/myOperation?param1=one&param2=two

Option 2

Use SOAPUI. It can generate SOAP messages for you, by reading your service's WSDL. My client's testers have been using it extensively with only a very broad understanding of web service technologies. An impressive tool.

Option 3

Groovy client (Same approach for other JVM based languages)

Use the wsdl2java tool to create a client stub class for the Shakespeare web service:

generate.sh:

$AXIS2_HOME/bin/wsdl2java.sh -d adb -s -o build -uri http://www.xmlme.com/WSShakespeare.asmx?WSDL
ant -file build/build.xml 

GetSpeech.groovy:

// Dependencies
// ============
import com.xmlme.webservices.ShakespeareStub

@Grapes([
    @Grab(group='org.apache.axis2', module='axis2-kernel', version='1.5.1'),
    @Grab(group='org.apache.axis2', module='axis2-adb', version='1.5.1'),
    @Grab(group='org.apache.axis2', module='axis2-transport-local', version='1.5.1'),
    @Grab(group='org.apache.axis2', module='axis2-transport-http', version='1.5.1'),
    @Grab(group='xerces', module='xercesImpl', version='2.6.2'),
    @GrabConfig(systemClassLoader=true)
])

// Main program
// ============
def stub = new ShakespeareStub()

// Request payload
def request = new ShakespeareStub.GetSpeech()
request.setRequest("Friends, romans, countrymen")

// Send request
response = stub.getSpeech(request)

println response.getGetSpeechResult()

Use the -cp parameter to add the generated code the the script's classpath

groovy -cp build/build/classes GetSpeech

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...