function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
GABRIEL SILVAGABRIEL SILVA 

call web service method from java and limit conection

Hello,

I'm trying to call my Apex web service method from java. The jar file was generated with enterprise.wsdl and wsc-39.0.4.jar. I created a simple java application and imported both jar files.

This is my simple web service on Apex:
 
global class webServiceBiel{
    
    WebService static integer soma(integer a, integer b){
        return a+b;
    }
}

I created a class in java to connect with my org. Here it is:
 
import com.sforce.soap.enterprise.Connector;
import com.sforce.soap.enterprise.EnterpriseConnection;
import com.sforce.soap.enterprise.QueryResult;
import com.sforce.soap.enterprise.sobject.Account;
import com.sforce.soap.enterprise.sobject.SObject;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;
import java.util.List;
import java.util.Arrays;

public class Main {

    static final String USERNAME = "gabriel.silva.dph@delphos.com.br.desenv";
    static final String PASSWORD = "Apex7410Z6iLE5wLGxxYbZsUY62U7lPC";
    static EnterpriseConnection connection;

    public static void main(String[] args) {

        ConnectorConfig config = new ConnectorConfig();
        config.setUsername(USERNAME);
        config.setPassword(PASSWORD);

        try {
            connection = Connector.newConnection(config);

            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());
            
            Biel_getContas();
            
        } catch (ConnectionException e1) {
            e1.printStackTrace();
        }
    }
    
    private static void Biel_getContas() {
        Account conta;
        System.out.println("Buscando as contas existentes...");
        
        try {    
            QueryResult queryResults = connection.query("SELECT Id, Name FROM Account");

            if (queryResults.getSize() > 0) {
                List<SObject> contas = Arrays.asList(queryResults.getRecords());
                for (SObject obj : contas) {
                    conta = (Account) obj;
                    System.out.println("ID = "+conta.getId());
                    System.out.println("Name = "+conta.getName());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

After running the application, I noticed that I have all the permissions on org, but I just want to connect with my web service and not have full access. How can i call WS method and block access to other things?
Raj VakatiRaj Vakati
Hi Gabriel, 

Remove "“API Enabled" permission to stop the connection. If you wanted to limit the data you can remove the object permission on the profile. 

Thanks ,
Raj


 
GABRIEL SILVAGABRIEL SILVA
I need call only the methods in the WS and bock others funcionalities, How can i do that?
GABRIEL SILVAGABRIEL SILVA
I generated the client of my service by netbeans and I called it. But now I'm having a connection error. Can anyone help?

My class now:
public static void main(String[] args) {

        ConnectorConfig config = new ConnectorConfig();
        config.setUsername(USERNAME);
        config.setPassword(PASSWORD);
        config.setAuthEndpoint(URL);
        config.setServiceEndpoint(URL);

        try {
            connection = Connector.newConnection(config);

            System.out.println("getAuthEndpoint: " + config.getAuthEndpoint());
            System.out.println("getServiceEndpoint: " + config.getServiceEndpoint());
            System.out.println("Username: " + config.getUsername());
            System.out.println("SessionId: " + config.getSessionId());

            int x = soma(4, 6);
            System.out.println("X = " + x);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static Integer soma(java.lang.Integer a, java.lang.Integer b) {
        client.WebServiceBielService service = new client.WebServiceBielService();
        client.WebServiceBielPortType port = service.getWebServiceBiel();
        return port.soma(a, b);
    }
}

The found error:
 
com.sun.xml.internal.ws.fault.ServerSOAPFaultException: Client received SOAP Fault from server: INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session Please see the server log to find more detail regarding exact cause of the failure.
	at com.sun.xml.internal.ws.fault.SOAP11Fault.getProtocolException(SOAP11Fault.java:178)
	at com.sun.xml.internal.ws.fault.SOAPFaultBuilder.createException(SOAPFaultBuilder.java:116)
	at com.sun.xml.internal.ws.client.sei.StubHandler.readResponse(StubHandler.java:238)
	at com.sun.xml.internal.ws.db.DatabindingImpl.deserializeResponse(DatabindingImpl.java:189)
	at com.sun.xml.internal.ws.db.DatabindingImpl.deserializeResponse(DatabindingImpl.java:276)
	at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:104)
	at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:77)
	at com.sun.xml.internal.ws.client.sei.SEIStub.invoke(SEIStub.java:147)
	at com.sun.proxy.$Proxy37.soma(Unknown Source)
	at client.Main.soma(Main.java:86)
	at client.Main.main(Main.java:38)