Access Salesforce Apex Soap Webservice from Java

Sebastian picture Sebastian · Jun 26, 2012 · Viewed 8.9k times · Source

i'm trying to establish a Connection to my Apex-Webservice but it fails everytime. My Webserce is quite Simple:

global class AtlassianService 
{
    webService static String hello(String Name) 
    {
        return 'Hello '+Name+' ! :D';
    }
}

To generate the Client i just the way, which is described here:

java –classpath pathToJAR/wsc-22.jar com.sforce.ws.tools.wsdlc pathToWsdl/WsdlFilename​ pathToOutputJar/OutputJarFilename

Accessing the Webservice:

SoapConnection soap = Connector.newConnection("[email protected]", "XXXX");

System.out.println(soap.hello("WORLD")); // Invalid Session ( => SessionID is null)

If I use the PartnerConnection to get a valid SessionID everything works fine:

ConnectorConfig config = new ConnectorConfig();
config.setUsername(username);
config.setPassword(password);
config.setAuthEndpoint("https://login.salesforce.com/services/Soap/u/24.0");

new PartnerConnection(config);

SoapConnection soap = Connector.newConnection(null, null);
soap.setSessionHeader(config.getSessionId());

System.out.println(soap.hello("WORLD"));

Anybody has an idea why the first example fails?

Greetings Sebastian

Answer

skyplanner picture skyplanner · Aug 19, 2012

I assume that happens because your AtlassianService wsdl, doesn't include the login method to authenticate in Salesforce, and then proxy classes generated by WSC cannot have the code to actually perform a login.

I was trying to do something very similar to your question (but with the Enterprise API). The solution I found was:

  1. Generate proxy classes using WSC for Enterprise.wsdl (enterprise.jar)
  2. Generate proxy classes using WSC for MyWebservice.wsdl (mywebservice.jar)
  3. Create the connection with enterprise, and get SessionId
  4. Set the SessionId in the request to MyWebservice
  5. Perform MyWebservice method call.

Like this:

import com.sforce.soap.MyWebservice.SoapConnection;
import com.sforce.soap.MyWebservice.Connector;

import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;
import com.sforce.soap.enterprise.*;


public class CallWS {


  static final String USERNAME = "username";
  static final String PASSWORD = "pass+securitytoken";

  static SoapConnection MyWebserviceWSconnection;
  static EnterpriseConnection enterpriseConnection;

  public static void main(String[] args) {

    ConnectorConfig config = new ConnectorConfig();
    config.setUsername(USERNAME);
    config.setPassword(PASSWORD);


    try {

      //create a connection to Enterprise API -- authentication occurs
      enterpriseConnection = com.sforce.soap.enterprise.Connector.newConnection(config);    
      // display some current settings
      System.out.println("Auth EndPoint: "+config.getAuthEndpoint());
      System.out.println("Service EndPoint: "+config.getServiceEndpoint());
      System.out.println("Username: "+config.getUsername());
      System.out.println("SessionId: "+config.getSessionId());


      //create new connection to exportData webservice -- no authentication information is included
      MyWebserviceWSconnection = Connector.newConnection("","");
      //include session Id (obtained from enterprise api) in exportData webservice
      MyWebserviceWSconnection.setSessionHeader(config.getSessionId());


      String result = MyWebserviceWSconnection.receiveData("test");
      System.out.println("Result: "+result);


    } catch (ConnectionException e1) {
        e1.printStackTrace();
    }  
  }
}