Create the apex class and generate its wsdl. Download Partner wsdl as well.
global class MathOperations
{
webservice static Integer getSum (Integer a, Integer b)
{
return a + b;
}
}
Convert the wsdl files that you have downloaded to jar files using the command below:
java -classpath force-wsc-40.1.1.jar;rhino-1.7.7.1.jar;ST-4.0.8.jar;C:\Java\jdk1.8.0_121\lib\tools.jar com.sforce.ws.tools.wsdlc partner.wsdl partner_stub.jar
Using SoapUI, get the sessionId from partner wsdl:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:partner.soap.sforce.com">
<soapenv:Header>
</soapenv:Header>
<soapenv:Body>
<urn:login>
<urn:username>username</urn:username>
<urn:password>password</urn:password>
</urn:login>
</soapenv:Body>
</soapenv:Envelope>
Enter the sessionId value in custom apex class wsdl. Check the below request and response:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:mat="http://soap.sforce.com/schemas/class/MathOperations">
<soapenv:Header>
<mat:SessionHeader>
<mat:sessionId>00D6A000001Vait!ARwAQPfREKhyBFwVu3F.vFEXYDvLIiR3w8K5hlZvemfv_vFPGECj.QLj3HXsOVFQzVb7OQAG.T9QFWqUzh5P0PP0rPhASOdo</mat:sessionId>
</mat:SessionHeader>
</soapenv:Header>
<soapenv:Body>
<mat:getSum>
<mat:a>5</mat:a>
<mat:b>8</mat:b>
</mat:getSum>
</soapenv:Body>
</soapenv:Envelope>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://soap.sforce.com/schemas/class/MathOperations">
<soapenv:Body>
<getSumResponse>
<result>13</result>
</getSumResponse>
</soapenv:Body>
</soapenv:Envelope>
You can call this web service using Java like below:
package wsc;
import com.sforce.soap.MathOperations.Connector;
import com.sforce.soap.MathOperations.SoapConnection;
import com.sforce.soap.partner.PartnerConnection;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;
public class SalesforceSoapStarter {
private static final String AUTH_ENDPOINT = "https://login.salesforce.com/services/Soap/u/40.0";
private static final String USERNAME = "username";
private static final String PASSWORD = "password";
public static void main(String[] args) throws ConnectionException {
ConnectorConfig config = new ConnectorConfig();
config.setUsername(USERNAME);
config.setPassword(PASSWORD);
config.setAuthEndpoint(AUTH_ENDPOINT);
PartnerConnection connection = new PartnerConnection(config);
String sessionId = config.getSessionId();
SoapConnection soap = Connector.newConnection("","");
soap.setSessionHeader(sessionId);
System.out.println(soap.getSum(4, 9));
}
}
Refer this link to set up Java developer environment:
https://developer.salesforce.com/docs/atlas.en-us.salesforce_developer_environment_tipsheet.meta/salesforce_developer_environment_tipsheet/salesforce_developer_environment_overview.htm