Jersey get, put, post, delete simple use, jerseyput

Source: Internet
Author: User

Jersey get, put, post, delete simple use, jerseyput

To use jersey, you must first have a corresponding dependency package. There are many methods to obtain the dependency package. Download the dependency file locally or obtain it using maven. Assume that your environment has been set up. To use jersey, you must first initialize a client. The following is the simplest get request.

// Initialize the client
ClientConfig cc = new DefaultClientConfig (); Client client = Client. create (cc); WebResource webResource = client. resource (url );

// Perform http Connection

ClientResponse response = webResource. get (ClientResponse. class );
Int status = response. getStatus (); // get the status code
String textEntity = response. getEntity (String. class); // get the returned content

The url can be the base address of the target address or target address (you can set a subaddress when accessing the url again)

 

First, describe some common operations.

// Insert parameters param1, param2
MultivaluedMap queryParams = new MultivaluedMapImpl (); queryParams. add ("param1", "val1"); queryParams. add ("param2", "val2"); ClientResponse = webResouce. queryParams (queryParams ). get (String. class );

2. http header settings. Baidu does not find many convenient settings. The following methods are for reference only.

// Add header information one by one using the header Method
ClientResponse response = webResource. header ("content-text", "application/json"). header ("", ""). get (ClientResponse. class );

3. the message body settings, such as post and put, can be used to pass an object. Here, the json object is used as an example.

JSONObject obj=new JSONObject();        try {            obj.put("username", "test8");            obj.put("password", "123456");        } catch (JSONException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }
ClientResponse response = webResource. header ("content-text", "application/json "). header ("",""). put (ClientResponse. class, obj); // Add the obj object to the parameter list
 

The get, put, post, and delete methods only modify webResource. put/get/post/delete ()

The following is a detailed explanation of the four methods.

Http://hugh-wangp.iteye.com/blog/1797237

 

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.