在Salesforce中能够建立Web Service供外部系统调用,而且能够以SOAP或者REST方式向外提供调用接口,下来的内容将详细讲述一下用SOAP的方式建立Web Service而且用java的程序进行简单的调用。java
【注:要想使其成为web service,那么class必定要定义成global的,具体的方法要用 webService static 修饰】web
在salesforce中开发-新建apex类。具体内容以下所示json
2):在保存好上述的class以后,咱们到setup --> build --> develop --> apex classes 中找到刚刚保存的class,咱们会发如今对应的Action中有WSDL这个选项,此选项就是Salesforce默认所提供的将Web Service的class转化成WSDL文件。以下图所示安全
3):点击上图的WSDL按钮,会看到以下界面,这里显示的是生成的WSDL文件的详细信息,咱们点击鼠标右键,将此文件保存到本地,这里姑且取名为AccountWebservice.wsdl测试
4):咱们能够简单的建立一个TestWebservice的javaprojectui
将AccountWebservice.wsdl生成AccountWebservice.jarspa
打开cmd 输入 java -classpath antlr-runtime-3.5.2.jar;tools.jar;st4-4.0.4.jar;force-wsc-45.1.0.jar com.sforce.ws.tools.wsdlc AccountWebservice.wsdl AccountWebservice.jarblog
将jar包导入项目中接口
代码以下,java中调用webservice中的接口方法ip
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.sforce.soap.AccountWebservice.Account;
import com.sforce.soap.AccountWebservice.SoapConnection;
import com.sforce.soap.enterprise.Connector;
import com.sforce.soap.enterprise.EnterpriseConnection;
import com.sforce.ws.ConnectorConfig;
import net.sf.json.JSONObject;
public class Test {
static final String USERNAME = "alice.zhu@slipstream.com.cn"; //Salesforce帐号中的用户名
static final String PASSWORD = "#########"; //密码,这个密码有点特殊,须要在密码后面加入安全标记
static EnterpriseConnection connection;
public static void main(String[] args) {
ConnectorConfig config = new ConnectorConfig();
config.setUsername(USERNAME);
config.setPassword(PASSWORD);
SoapConnection sc =null;
try {
connection = Connector.newConnection(config);
config.setServiceEndpoint("https://ap8.salesforce.com/services/Soap/class/AccountWebservice");
sc = new SoapConnection(config);
Account[] ss=sc.getAccountList();
List<Account> resultList = new ArrayList<>(ss.length);
for (Account account : ss) {
resultList.add(account);
}
Map<String,Object> map = new HashMap<>();
map.put("total", resultList.size());
map.put("rows", resultList);
JSONObject jsons = JSONObject.fromObject(map);
System.out.println(jsons.toString());
}catch(Exception e) {
e.printStackTrace();
}
}
}
测试