function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kevin WrightKevin Wright 

Trying to call a simple Apex Web Service method from Java

Hi Guys,

I am attempting to call a simple web service that I have written in Apex from a Java Program.

The steps I have taken so far.

1. Create a webservice method inside a global class. (compiles ok).
2. Generate wsdl files for both my WebService and the org.
3. Install Apache Axis
4. Use Axis to generate proxy stubs (runs ok - and generates Java source files.)
5. Compile All Java sources generated (all compile first time without changes).
6. Construct Client code (as shown below).

import com.sforce.soap.schemas._class.MyWebService.*;
import java.net.*;


public class TestCall{

 

 public static void main(String[] args){

  System.out.println("start of program");
  try{

                 //        SoapBindingStub  binding = (SoapBindingStub) new SforceServiceLocator().getSoap();

   MyWebServiceBindingStub stub =
                              new MyWebServiceBindingStub
                               (new URL("https://na1-api.salesforce.com/services/Soap/class/MyWebService"),null);
   //com.sforce.soap.schemas._class.MyWebService.MyWebServicePortType pt =
                                //  stub.getMyWebService();
   System.out.println("about to make call");
   stub.myMethod();
  }
  catch(Exception e){
   System.out.println("exception thrown");
   System.out.println(e.getMessage());
  }
 }

 


}


7. The code above compiles fine.
8. On running I get:

INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session.

Can you please tell me where I am going wrong. (While I still have some hair left to pull out please :)  )

Thank you , in advance.


Kevin Wright.

Best Answer chosen by Admin (Salesforce Developers) 
Abhinav GuptaAbhinav Gupta

SoapBindingStub should be available to you, it comes with both partner & enterprise wsdls. 

Here is the fully qualified name of the classes.

com.sforce.soap.enterprise.SoapBindingStub

com.sforce.soap.partner.SoapBindingStub

All Answers

_Prasu__Prasu_

You will need to make a Login call to Salesforce first.

Login results return by the login call will contain the SFURL and SFsession Id.

 

Above two values should be set in the header for the consuming the web-service hosted in the Salesforce.

Kevin WrightKevin Wright

Hi Prasanna,

 

In order to call the login method, I see I need a class called SoapBindingStub which I dont have on my classpath. Can you tell me which Jar file holds this?.

Abhinav GuptaAbhinav Gupta

SoapBindingStub should be available to you, it comes with both partner & enterprise wsdls. 

Here is the fully qualified name of the classes.

com.sforce.soap.enterprise.SoapBindingStub

com.sforce.soap.partner.SoapBindingStub

This was selected as the best answer
Kevin WrightKevin Wright

Hi Abhinav,

 

Yes you're right. I used the wrong org wsdl (apex instead of enterprise). This gets me a step further. At least I my call to

login compiles now...

 

A big thanks...

 

Kevin

Naved_GESDOMNaved_GESDOM

I am also trying to call apex web service, I am using SOAPUI but it is not accepting the wsdl doc giving null pointer exception, My target namespace is targetNamespace="http://soap.sforce.com/schemas/class/MyWebService", Web service is on my developers account.

 

Can anyone please help.

 

Thanks in advance.....!!!!!

Naved_GESDOMNaved_GESDOM

Please can anyone help me on this..

goabhigogoabhigo

Can you post the final code here? I am interested in code that calls the custom web service method. Also can you provide what other codes you used to call the method?