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