Access Salesforce Apex Soap Webservice from Java

I try to establish a connection with my Apex-Webservice, but it fails every time. My web service is pretty simple:

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

To generate Client, I am as described here :

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

Access to Webservice:

 SoapConnection soap = Connector.newConnection(" mail@XXXXXX.com ", "XXXX"); System.out.println(soap.hello("WORLD")); // Invalid Session ( => SessionID is null) 

If I use 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")); 

Does anyone have an idea why the first example fails?

Greetings of Sebastian

+4
source share
2 answers

I assume this is because your AtlassianService wsdl does not include a login method for authenticating with Salesforce, and then the proxy classes generated by WSC cannot have the code to actually complete the login.

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

  • Creating proxy classes using WSC for Enterprise.wsdl (enterprise.jar)
  • Creating proxy classes using WSC for MyWebservice.wsdl (mywebservice.jar)
  • Create an enterprise connection and get a SessionId
  • Set SessionId in MyWebservice request
  • Call the MyWebservice method.

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

each wsc operation call throws a ConnectionException or one of its specific subtypes in case of an error. therefore, you should receive an error message that is likely to help solve the problem. To debug soap messages sent and received by wsc, add the following lines after initializing your ConnectorConfig:

 conf.setPrettyPrintXml(true); conf.setTraceMessage(true); 

This will print all the xml exchanged between your client and the web service. Probably the answer will also help you find the cause of your problem.

Hope this helps. h9nry

0
source

All Articles