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

前端 未结 2 1526
情歌与酒
情歌与酒 2020-12-02 00:46

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

**

Axis2, Eclipse, Tomcat, Ant

**

I need to creat

相关标签:
2条回答
  • 2020-12-02 01:18

    If you have access to the WSDL,the following code/JAX-WS client can be used to invoke any SOAP based web service.

    import java.net.URL;
    import javax.xml.namespace.QName;
    import javax.xml.ws.Service;
    
    public class WebserviceClient {
    
        public static void main(String[] args) throws Exception {
    
            URL url = new URL
                    ("http://localhost:9999/ws/additionService?wsdl");
    
            QName qname = new QName("http://test/", 
                    "AdditionServiceImplService");//Line 2
    
            Service service = Service.create(url, qname);
    
            AdditionService additionService = service
                    .getPort(AdditionService.class);
    
            System.out.println(additionService.add(1, 2));
    
        }
    
    }
    

    In Line 2,QName first argument is the namespace used in WSDL and second argument is simply the service name.

    0 讨论(0)
  • 2020-12-02 01:32

    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
    
    0 讨论(0)
提交回复
热议问题