Access Salesforce Apex Soap Webservice from Java

apex-code, java, salesforce, soap, web-services

Solution

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:

- Generate proxy classes using WSC for Enterprise.wsdl (enterprise.jar)

- Generate proxy classes using WSC for MyWebservice.wsdl (mywebservice.jar)

- Create the connection with enterprise, and get SessionId

- Set the SessionId in the request to MyWebservice

- 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();
    }  
  }
}

Problem

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("mail@XXXXXX.com", "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

Original source