[SalesForce] using SOBJECT Tree resource in REST API Call

I Was of view, through rest API , only single record can be created in SFDC
but this link has left me intrigued

Create Multiple records Via Rest

Through CURL using above tree resource, I'm able to create multiple records via Rest, at once .
But I've question, how can I use this tree resource in JAVA Program to create records? Is this possible ?

I was trying something like this :

  JSONParser parser = new JSONParser();

  // Parser parsing  JSON File having multiple records
 Object obj = parser.parse(new FileReader("C:/Users/ABCuser/Documents/CURL/NewAccount.JSON"));

   httpclient = new HttpClient();
   PostMethod mypost = new PostMethod(uri);
   String jsonText = obj.toString();
   System.out.println("\n_JSON TEXT=" + jsonText);
   mypost.setRequestHeader("Authorization", "OAuth " + accessToken); 
   mypost.setRequestEntity(new StringRequestEntity(jsonText, "application/json", "UTF-8"));
   int response=httpclient.executeMethod(mypost);

At last , it just throws error with error code 404:

[{"errorCode":"NOT_FOUND","message":"The requested resource does not
exist"}]

my question : My reason is, if this works in CURL, then it should be working in JAVA also, but I don't know, how I should proceed.

Best Answer

This is possible in java. I tried your code and it is working. As per my observation only thing which could be culprit here is the uri. Below is the code snippet where I have mentioned uri directly.

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;


public class Main {

        public static void main(String[] args) throws FileNotFoundException, IOException, ParseException {
            JSONParser parser = new JSONParser();

               // Parser parsing  JSON File having multiple records
               Object obj = parser.parse(new FileReader("JsonData.json"));

               HttpClient httpclient = new HttpClient();
               PostMethod mypost = new PostMethod("https://kmforce-dev-ed.my.salesforce.com/services/data/v34.0/composite/tree/Account/");
               String jsonText = obj.toString();
               System.out.println("\n_JSON TEXT=" + jsonText);
               mypost.setRequestHeader("Authorization", "OAuth " +"<myssessionid>"); 
               mypost.setRequestEntity(new StringRequestEntity(jsonText, "application/json", "UTF-8"));
               int response=httpclient.executeMethod(mypost);
               System.out.println(response);
               System.out.println("Response-->"+mypost.getResponseBodyAsString());
        }
}

Below is the output:

{
    "hasErrors": false,
    "results": [
        {
            "referenceId": "ref1",
            "id": "0019000001bslYJAAY"
        },
        {
            "referenceId": "ref2",
            "id": "0019000001bslYKAAY"
        },
        {
            "referenceId": "ref3",
            "id": "0019000001bslYLAAY"
        },
        {
            "referenceId": "ref4",
            "id": "0019000001bslYMAAY"
        }
    ]
}

Let me know if you face any issue here.