Can i use Axis2 to write a client for SalesForce API?
Yes. Just run WSDL2Java with the following Options (with the forthcoming Axis2 1.3 Final):
wsdl2java -Eofv -g -uw -u -uri enterprise.wsdl
Notes:
- -Eofv : tells wsdl2java to be a little lax with the schema validation
- -g : tells wsdl2java generate all the classes specified in the schema, since usually WSDL2Java with ADB databinding generates only those complextypes / elements that are actually used in the wsdl
- -uw : Unwrap the parameters, makes it a little bit easier to write code
- -u : Unpacks the databinding classes, since by default when generating clients WSDL2Java generates all data types in the same Stub class
- -uri : location of the wsdl
Here's some sample code:
import com.sforce.soap.enterprise.LoginResult;
import com.sforce.soap.enterprise.QueryResult;
import com.sforce.soap.enterprise.SessionHeader;
import com.sforce.soap.enterprise.SforceServiceStub;
import com.sforce.soap.enterprise.sobject.Account;
import com.sforce.soap.enterprise.sobject.SObject;
import org.apache.axis2.client.Options;
import org.apache.axis2.transport.http.HTTPConstants;
public class Main {
public static void main(String[] args) throws Exception {
SforceServiceStub stub = new SforceServiceStub();
Options options = stub._getServiceClient().getOptions();
options.setProperty(HTTPConstants.MC_ACCEPT_GZIP, Boolean.TRUE);
options.setProperty(HTTPConstants.MC_GZIP_REQUEST, Boolean.TRUE);
LoginResult lr = stub.login("my@gmail.com", "abcdef", null);
SessionHeader sh = new SessionHeader();
sh.setSessionId(lr.getSessionId());
stub = new SforceServiceStub(lr.getServerUrl());
options = stub._getServiceClient().getOptions();
options.setProperty(HTTPConstants.MC_ACCEPT_GZIP, Boolean.TRUE);
options.setProperty(HTTPConstants.MC_GZIP_REQUEST, Boolean.TRUE);
QueryResult qr = stub.query("select Name, numberOfEmployees, Id, Industry from Account",
sh, null, null);
System.out.println("Query has " + qr.getSize() + " records total");
SObject[] sObjects = qr.getRecords();
for (int i = 0; i < sObjects.length; i++) {
Account sObject = (Account) sObjects[i];
System.out.println(i + "\t: [" + sObject.getId() + "][" +
sObject.getName() + "]");
}
}
}
Here's the sample output:
C:\axis2-1.3\sforce\client>java Main
Query has 13 records total
0 : [0017000000Kn04OAAR][GenePoint]
1 : [0017000000Kn04PAAR][United Oil & Gas, UK]
2 : [0017000000Kn06qAAB][United Oil & Gas, Singapore]
3 : [0017000000Kn06rAAB][Edge Communications]
4 : [0017000000Kn06sAAB][Burlington Textiles Corp of America]
5 : [0017000000Kn06tAAB][Pyramid Construction Inc.]
6 : [0017000000Kn06uAAB][Dickenson plc]
7 : [0017000000Kn06vAAB][Grand Hotels & Resorts Ltd]
8 : [0017000000Kn06wAAB][Express Logistics and Transport]
9 : [0017000000Kn06xAAB][University of Arizona]
10 : [0017000000Kn06yAAB][United Oil & Gas Corp.]
11 : [0017000000Kn06zAAB][sForce]
12 : [0017000000KmzIhAAJ][ABCED]
- dims's blog
- Login or register to post comments
- Printer friendly version
- 1332 reads










