rest - accessing webservice using jersey client -
package java4s; import javax.ws.rs.post; import javax.ws.rs.path; import javax.ws.rs.pathparam; import javax.ws.rs.produces; import javax.ws.rs.core.mediatype; @path("/vinay") public class jsonfromrestful { @post @path("/{runid}/{tweetid}/{tweet : .*}") @produces(mediatype.text_plain) public string sayplaintexthello( @pathparam("runid") string rid, @pathparam("tweetid") string tid, @pathparam("tweet") string twt) throws ioexception, interruptedexception { stringbuffer resneel=new stringbuffer(); resneel.append(rid); resneel.append(tid); resneel.append(twt); return return resneel.tostring(); } } client test program package neel; import javax.ws.rs.core.mediatype; import javax.ws.rs.core.multivaluedmap; 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.core.util.multivaluedmapimpl; /* * excerpt of maven dependencies <dependency> <groupid>com.sun.jersey</groupid> <artifactid>jersey-client</artifactid> <version>1.19</version> </dependency> <dependency> <groupid>com.sun.jersey</groupid> <artifactid>jersey-core</artifactid> <version>1.19</version> </dependency> */ public class clienttest { public static void main(string[] args) { if(args.length != 4) { system.out.println("incorrect parameters, usage:"); system.out.println("java -jar neelclienttest.jar run_id tweet_id tweet_to_annotate rest_api"); system.exit(1); } string runid = args[0]; string tweetid = args[1]; string tweet = args[2]; string uri = args[3]; try { string annotations = annotate(uri, runid, tweetid, tweet); system.out.println(annotations); } catch (exception e) { e.printstacktrace(); } } public static string annotate(string uri, string runid, string tweetid, string tweet) throws exception { client client = client.create(); webresource webresource = client.resource(uri); multivaluedmap<string,string> params = new multivaluedmapimpl(); params.add("runid", runid); params.add("tweetid", tweetid); params.add("tweet", tweet); clientresponse response = webresource. accept(mediatype.text_plain_type). post(clientresponse.class, params); // check if status code != 200 ok if ( response.getstatus() != 200 ) throw new exception ("the rest interface has answered unexpected status code" + response.getstatus()); return response.getentity(string.class); } } here giving command line arguements runid tweetid tweet , path http://localhost/cen_neel/vinay/ showing java.lang exception. created webservice , whatever have given clienttest testing. dont need changes in clienttest. please if there error in program. tested same program restclient plugin below http://localhost/cen_neel/vinay/r12/v23/chennai , getting response correctly.
here post(clientresponse.class, params);. you're trying post uri information body of request. that's not belong. actually, based on resource method, there shouldn't body @ all. can pass empty string.
instead, how should building request, .path() on webresource append path segments
clientresponse response = webresource .path(runid) .path(tweetid) .path(tweet) .accept(mediatype.text_plain_type) .post(clientresponse.class, ""); it seems pointless though, way doing it. why not pass entire uri, in rest client.
anyway, not design overall. here improvements make.
the actual tweet should not in uri. should posted in body. accept in body, don't annotate
@pathparamtweetmethod parameter, , rid of path template{tweet : .*}. canpost(clientreponse.class, tweet). should add@consumes(mediatype.text_plain)resource method, , usetypecreate request. i.e..accept(mediatype.text_plain_type) .type(mediatype.text_plain_type) .post(clientresponse.class, tweet);if trying create new tweet, id, should not in template. id should not exist yet. when tweet created, server should notify client newly created id of tweet,
locationheader.
Comments
Post a Comment