前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >在Salesforce中创建Web Service供外部系统调用

在Salesforce中创建Web Service供外部系统调用

作者头像
故久
发布2019-09-29 14:51:26
1.2K0
发布2019-09-29 14:51:26
举报
文章被收录于专栏:故久故久故久

在Salesforce中可以创建Web Service供外部系统调用,并且可以以SOAP或者REST方式向外提供调用接口,下来的内容将详细讲述一下用SOAP的方式创建Web Service并且用java的程序进行简单的调用。

【注:要想使其成为web service,那么class一定要定义成global的,具体的方法要用 webService static 修饰】

在salesforce中开发-新建apex类。具体内容如下所示

2):在保存好上述的class之后,我们到setup --> build --> develop --> apex classes 中找到刚刚保存的class,我们会发现在对应的Action中有WSDL这个选项,此选项就是Salesforce默认所提供的将Web Service的class转化成WSDL文件。如下图所示

3):点击上图的WSDL按钮,会看到如下界面,这里显示的是生成的WSDL文件的详细信息,我们点击鼠标右键,将此文件保存到本地,这里姑且取名为AccountWebservice.wsdl

4):我们可以简单的创建一个TestWebservice的javaproject

将AccountWebservice.wsdl生成AccountWebservice.jar

打开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.jar

将jar包导入项目中

代码如下,java中调用webservice中的接口方法

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

}

测试

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档