Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to call SAP web service in Java?

Former Member
0 Kudos
4,742

I have a RFC-enabled function in sap (WebService).. I want to call this function and get the result using java. What should I do? (I already have the WSDL endpoint)

1 ACCEPTED SOLUTION

rosenberg_eitan
Active Contributor
0 Kudos
897

Hi ,

If you use eclipse you can use the built in facility to consume the web service (Use the EE version).

The wsdl will be used as input -

Search the web how to create web service client in eclipse .

To test the service you can use soapui http://www.soapui.org/

Also look at Apache Axis2 - Apache Axis2/Java - Next Generation Web Services

Regards.

6 REPLIES 6

rosenberg_eitan
Active Contributor
0 Kudos
898

Hi ,

If you use eclipse you can use the built in facility to consume the web service (Use the EE version).

The wsdl will be used as input -

Search the web how to create web service client in eclipse .

To test the service you can use soapui http://www.soapui.org/

Also look at Apache Axis2 - Apache Axis2/Java - Next Generation Web Services

Regards.

0 Kudos
897

Hi,

Thanks for your reply, but I want to create a Java application, not a web application .. does it work too ?

0 Kudos
897

Hi,

You just need the generated Java Code .

.

It is a regular desktop app .

former_member438956
Active Participant
0 Kudos
897

Hi,

Please run the below java program to test the web service.

you can specify your end point details and execute your method.

package com.an.edu.SOAPtest;

import javax.xml.soap.*;

import javax.xml.transform.*;

import javax.xml.transform.stream.*;

public class SOAPClientSAAJ {

    /**

     * Starting point for the SAAJ - SOAP Client Testing

     */

    public static void main(String args[]) {

        try {

            // Create SOAP Connection

            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();

            SOAPConnection soapConnection = soapConnectionFactory.createConnection();

            // Send SOAP Message to SOAP Server

            String url = "http://ws.cdyne.com/emailverify/Emailvernotestemail.asmx";

            SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);

            // Process the SOAP Response

            printSOAPResponse(soapResponse);

            soapConnection.close();

        } catch (Exception e) {

            System.err.println("Error occurred while sending SOAP Request to Server");

            e.printStackTrace();

        }

    }

    private static SOAPMessage createSOAPRequest() throws Exception {

        MessageFactory messageFactory = MessageFactory.newInstance();

        SOAPMessage soapMessage = messageFactory.createMessage();

        SOAPPart soapPart = soapMessage.getSOAPPart();

        String serverURI = "http://ws.cdyne.com/";

        // SOAP Envelope

        SOAPEnvelope envelope = soapPart.getEnvelope();

        envelope.addNamespaceDeclaration("example", serverURI);

        // SOAP Body

        SOAPBody soapBody = envelope.getBody();

        SOAPElement soapBodyElem = soapBody.addChildElement("VerifyEmail", "example");

        SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("email", "example");

        soapBodyElem1.addTextNode("nautiyal.anil@gmail.com");

        SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("LicenseKey", "example");

        soapBodyElem2.addTextNode("123");

        MimeHeaders headers = soapMessage.getMimeHeaders();

        headers.addHeader("SOAPAction", serverURI  + "VerifyEmail");

        soapMessage.saveChanges();

        /* Print the request message */

        System.out.print("Request SOAP Message = ");

        soapMessage.writeTo(System.out);

        System.out.println();

        System.out.print("Getting SOAP response");

        return soapMessage;

    }

    /**

     * Method used to print the SOAP Response

     */

    private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception {

    System.out.print("Getting SOAP response");

        TransformerFactory transformerFactory = TransformerFactory.newInstance();

        Transformer transformer = transformerFactory.newTransformer();

       

        Source sourceContent = soapResponse.getSOAPPart().getContent();

        System.out.print("\nResponse SOAP Message = ");

        StreamResult result = new StreamResult(System.out);

        transformer.transform(sourceContent, result);

    }

}

Regards,

Anil N.

0 Kudos
897

Hi,

Thank you for you reply, I have already could print the response xml, but how parse the result rather than only display the xml ?

0 Kudos
897

Better post in some Java forum you will get better help there.