Jersey Framework put request to send JSON data

Source: Internet
Author: User

Want to send a POST request with the Jersey framework, body with JSON format parameter

Url:http//192.168.1.211/api/test

The body data is {"Action": "Test"}

The implementation is as follows:

Note: The reference jar package must be correct, or debug a bunch of error.

The following packages are quoted here:

Jettison-1.3.jar This provides jsonobject if you use the other may report various errors.

Jersey-bundle-1.9.jar This is Jersey bundle, a problem solving

Javax.ws.rs.jar This provides mediatype type

Import Javax.ws.rs.core.mediatype;import Org.codehaus.jettison.json.jsonexception;import Org.codehaus.jettison.json.jsonobject;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.config.clientconfig;import Com.sun.jersey.api.client.config.DefaultClientConfig; public class Restclient {clientconfig cc = new Defaultclientconfig (); Client client = Client.create (cc); WebResource resource = Client.resource ("http://192.168.1.211/api/instance/6/3/"); Jsonobject obj = new Jsonobject (); try {obj.put ("action", "Start"),} catch (Jsonexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} Clientresponse response = resource.accept (Mediatype.application_json). Type (Mediatype.application_json). Post ( Clientresponse.class, obj); String addresses = response.getentity (String.class); SYSTEM.OUT.PRINTLN (addresses);}



Jersey Framework put request to send JSON data

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.