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