java equivalent for curl post command

I am looking for an equivalent in java for the following  curl command. I know I can do a Runtime.exec() etc to run this, but I was looking for a way to do this using some Jersey client API or some other similar solutions.

curl -v \
--user admin:admin123 \
-H X-Requested-By:MyClient \
-H Accept:application/json \
-H Content-Type:application/json \
-d "{"name":"Partition0",
"properties" : [
{ "name" : "resourceGroups",
        "properties" : [
                { "name" : "g1",
                "properties" : [
                        { "name" : "resourceGroupTemplate", "value" : "template1" },
                        { "name" : "targets" , "value" : "VirtualTarget-0"}]}]},
{"name" : "availableTargets" , "value" : "VirtualTarget-0"},
{"name" : "defaultTarget" , "value" : "VirtualTarget-0"},
{"name" : "securityRealm" , "value" : "myrealm"}
]}" \
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You can use libcurl java bindings.
ctoanAuthor Commented:
I did consider libcurl, but I heard the code may not be portable and may not work on all platforms. Moreover, I don't see any good examples of libcurl with Java.
Why you do all that in CURL firsthand? Java has it's web services and restful services APIs builtin?
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

ctoanAuthor Commented:
Well, I'm new to this and that's why I want to know how this can be done in Java. I got this curl command and would like to know the Java equivalent. I have seen examples using Jersey Client API and HttpClient etc, but I'm not sure how this particular example I posted can be converted.

it is done using official APIs, like jersay client api you mentioned, completely abstracting from having linux or windows or solaris, using http/1.0 or  http/2.0 over ipv4 or ipv6, using SSL or not....
ctoanAuthor Commented:
The example at the following link (with some changes) helped me to get this working

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;

public class JerseyClientPost {

      public static void main(String[] args) {

            try {

                  Client client = Client.create();
                  client.addFilter(new HTTPBasicAuthFilter("admin","admin123"));

                  WebResource webResource = client
                  String input = ""{"name\":"Partition0\",\"properties\" : [{ \"name\" : \"resourceGroups\",\"properties\" : [{ \"name\" : \"g1\", ....... ";

                  JSONObject json = new JSONObject(input);

                  ClientResponse response = webResource.type("application/json")
                              .header("X-Requested-By", "MyClient")
                              .header("Accept", "application/json")
                              .post(ClientResponse.class, json.toString());

                  if (response.getStatus() != 201) {
                        throw new RuntimeException("Failed : HTTP error code : "
                                    + response.getStatus()+ response.toString());

                  System.out.println("Output from Server .... \n");
                  String output = response.getEntity(String.class);

            } catch (Exception e) {





Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
You should use JSR 353 API and not call jetson classes directly. Sooner or later (read java 9 or Java 10) such sidestep will be punished with deprecation. At that moment you will have too much codebase to change it to normal API and your app will be stuck with old java version (like java 5 is burried end of may, and java7 gets costly in september)
ctoanAuthor Commented:
Didn't really receive any useful information from anyone else. All the info provided was already stated in my question.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.