gpt4 book ai didi

java - 从 Java 访问 Salesforce Apex Soap Web 服务

转载 作者:搜寻专家 更新时间:2023-11-01 01:45:45 25 4
gpt4 key购买 nike

我正在尝试与我的 Apex-Web 服务建立连接,但每次都失败。我的 Webserce 非常简单:

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

生成Client i 就是这样,描述为here :

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

访问网络服务:

SoapConnection soap = Connector.newConnection("mail@XXXXXX.com", "XXXX");

System.out.println(soap.hello("WORLD")); // Invalid Session ( => SessionID is null)

如果我使用 PartnerConnection 获取有效的 SessionID,一切正常:

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"));

有人知道第一个示例失败的原因吗?

问候塞巴斯蒂安

最佳答案

我假设发生这种情况是因为您的 AtlassianService wsdl 不包含在 Salesforce 中进行身份验证的登录方法,然后 WSC 生成的代理类无法包含实际执行登录的代码。

我正在尝试做一些与您的问题非常相似的事情(但使用企业 API)。我找到的解决方案是:

  1. 使用 WSC 为 Enterprise.wsdl (enterprise.jar) 生成代理类
  2. 使用 WSC 为 MyWebservice.wsdl (mywebservice.jar) 生成代理类
  3. 创建与企业的连接,并获取SessionId
  4. 将请求中的SessionId设置为MyWebservice
  5. 执行 MyWebservice 方法调用。

像这样:

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

关于java - 从 Java 访问 Salesforce Apex Soap Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11204614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com