Calling restful Web services through the Jersey Client API

Source: Internet
Author: User
Tags http post

Jersey Client API Basics
To get started with the Jersey client API, you first need to create an instance of the Com.sun.jersey. Api.client.Client class. Here's the simplest approach:

Import Com.sun.jersey. api.client.Client;
Client client = Client.create ();
The client class is the primary configuration point for creating a RESTful Web Service client. You can use it to configure different client properties and features, and to indicate which resource provider to use. Creating an instance of a client class is a relatively expensive operation, so try to avoid creating unwanted client instances. The better way is to reuse the existing instances as much as possible.

Once you have created an instance of a Client class, you can start using it. In any case, before making a request, you need to create a Web Resource object to encapsulate the Web resources required by the client.

The WEB resource creates a WebResponse object:

Import Com.sun.jersey. Api.client.WebResource;
Web Resource WebResource = C.resource ("http://example.com/base");
You create a request to send to a Web resource by using the WebResource object, and you handle the response that is returned from the Web resource. For example, you can use the WebResource object to send HTTP GET, PUT, POST, and DELETE requests.

GET request: Use the Get () method of the WebResource class to submit an HTTP GET request to the WEB resource:

String s = webresource.get (String.class);
This means that if the URL of the WebResource object is http://example.com/base, an HTTP GET request will be sent to the address http://example.com/base of resources.

String s = webresource.get (String.class);
You can also specify the query parameters when a get () request is requested. For example, the following code specifies two query parameters in a get () Request:
Multivaluedmap queryparams = new Multivaluedmapimpl ();
Queryparams.add ("param1", "val1");
Queryparams.add ("param2", "val2");
String s = webresouce.queryparams (Queryparams). get (String.class);

You can also specify the MIME type that the response can accept. For example, the following code specifies that the MIME type of the response can only be text:

String s = webresource.accept ("Text/plain"). Get (String.class);

You can also get the HTTP status code for the corresponding request, such as the following example to get the text entity and status code returned by a request:

Clientresponse response = webresource.accept ("Text/plain")
. get (Clientresponse.class);
int status = Response.getstatus ();
String textentity = response.getentity (String.class);

The Clientresponse object represents an HTTP response received by a client.

Put request: Use the Put () method of the WebResource class to submit an HTTP put request to a WEB resource. For example, the following code shows a request to send a text entity Foo:bar to the specified Web resource:

Clientresponse response = Webresource.type ("Text/plain")
. put (Clientresponse.class, "Foo:bar");

Similarly, you can specify query parameters when you send a request using the put () method, as you would specify a query parameter when using the Get () method. In the following example, the two same query parameters used in the previous get () Method example are assigned to a put () Request:

Multivaluedmap queryparams = new Multivaluedmapimpl ();
Queryparams.add ("param1", "val1");
Queryparams.add ("param2", "val2");
Clientresponse response = webresource.queryparams (queryparams)
                                       .put (ClientResponse.class, "Foo:bar");
 post Request: A POST request is a combination of a GET request and a PUT request, which means that you can use a POST request to send an entity to the specified WEB resource and receive another entity. Use the Post () method of the WebResource class to send an HTTP POST request to the specified Web resource. The following example shows a POST request to send a form data with query parameters and URL encoding:

Multivaluedmap formData = new Multivaluedmapimpl ();
Formdata.add ("name1", "val1");
Formdata.add ("name2", "val2");
Clientresponse response = Webresource.type ("application/x-www-form-urlencoded")
                                       .post ( Clientresponse.class, FormData);
 delete Request: Use the DELETE () method of the Web Resource class to send the Jean on the HTTP delete request to the specified Web resource. For example, the following example shows the deletion of a URI of  http://example.com/base/user/123   Resources:

Clientresponse response = Webresource.path ("user/123")
. Delete (Clientresponse.class);
In addition, the Web Resource.path () method can be used in all HTTP requests, which allows you to specify an additional path for the Web resource to be requested. Another method header () of the Webresouce class can add HTTP header information to your request.

In addition, if the form is submitted, a new form is required as a parameter submission.

An example based on the Jersey client

Package com.trend.vmware.client;

Import Javax.ws.rs.core.MediaType;

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.representation.Form;
Import Com.trend.vmware.model.TaskResponse;


public class Vmovfrestclient {


public static void Main (string[] args) {
String Resturi = "Http://localhost:8080/VmService";
Client C = client.create ();

WebResource WebResource = C.resource (resturi+ "/rest/vsphere/template");
EXPORTOVF (WebResource, "autolab/test_vm3/action/exportovf");
IMPORTOVF (WebResource, "10.64.3.188/action/importovf");

}

public static void exportovf (WebResource R, String Pathparam) {
Postform (R.path (Pathparam), "Datastore1", "d:/ovf");
System.out.println ("-------------------------" +r.path (Pathparam));

}

public static void importovf (WebResource R, String Pathparam) {
POSTFORMFORIMPORTOVF (R.path (Pathparam), "e:/ovf", "Datastore1", "TEST_VM");
System.out.println ("-------------------------" +r.path (Pathparam));

}

To POST FORM
public static void Postform (WebResource R, String datastorename,string LocalPath) {
Form form = new form ();
Form.add ("Datastorename", datastorename);
Form.add ("LocalPath", LocalPath);
Clientresponse response = R.type (mediatype.application_form_urlencoded)
. Post (Clientresponse.class, form);
TODO Handler Response.getentity
System.out.println (Response.getentity (String.class));
}
To POST FORM for IMPORTOVF
public static void postformforimportovf (WebResource R, String localpath,string datastorename,string newvmname) {
Form form = new form ();
Form.add ("LocalPath", LocalPath);
Form.add ("Datastorename", datastorename);
Form.add ("Newvmname", newvmname);
Clientresponse response = R.type (mediatype.application_form_urlencoded)
. Post (Clientresponse.class, form);
TODO Handler Response.getentity

Taskresponse res = response.getentity (taskresponse.class);
SYSTEM.OUT.PRINTLN ("Res---" +res.getmessage () + "-" +res.gettaskstatus ());
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.