java - How to send requests and receive responses to/from an Apache CXF-based service? -


i have several servlets,

  1. take json-encoded requests inputs,
  2. process them and
  3. return responses client json-encoded objects.

up used android client (sample android code see below).

now want write plain old java program, send requests , receive responses (do same android code). purpose wrote java test (code see below, section java code) , ran it.

at client side error:

21:43:38.930 [main] error r.a.c.t.testacceptanceprocedure1 -  java.io.ioexception: server returned http response code: 405 url: http://myserver/myapp/rest/getuseridservice     @ sun.net.www.protocol.http.httpurlconnection.getinputstream(httpurlconnection.java:1441) ~[na:1.6.0_23]     @ testclient.testacceptanceprocedure1.test(testacceptanceprocedure1.java:47) ~[test-classes/:na] 

in server log, see message:

warning: no operation matching request path "/myapp/rest/getuseridservice" found, relative path: /, http method: get, contenttype: */*, accept: text/html,image/gif,image/jpeg,*/*,*/*;q=.2,. please enable fine/trace log level more details 

question: how should change java test fix error?

note server , running (when execute android code, works).

android code:

sending request , receiving response:

final getsimulationstatusrequest request = new getsimulationstatusrequest(); final string json = utils.converttojson(request, getclass()); final string serverurl = utils.getserverurl(this, "getsimulationstatusservice");  final igetsimulationstatusasynctask getsimulationstatustask =          asynctaskfactory.creategetsimulationstatusasynctask(getwebservicehelper());  utils.setrequestandserverurl(json, serverurl, getsimulationstatustask);  final getsimulationstatusresponse simulationstatusreponse =          getsimulationstatustask.get(); 

utils.converttojson:

public static string converttojson(final object arequest, class<? extends activity> aclass) {     final objectmapper mapper = new objectmapper();     string json = null;      try {         json = mapper.writevalueasstring(arequest);     } catch (final jsonprocessingexception exception) {         log.e(aclass.getsimplename(), exception.getlocalizedmessage(),                 exception);     }     return json; } 

utils.setrequestandserverurl:

public static void setrequestandserverurl(final string ajson,         final string aserverurl, final iasynctask atask) {     atask.addnamevaluepair("request", ajson);     atask.sendrequest(new string[] { aserverurl }); } 

getsimulationstatusasynctask:

public class getsimulationstatusasynctask      extends asynctask<string, string, getsimulationstatusresponse>      implements igetsimulationstatusasynctask {     private static final string tag = getsimulationstatusasynctask.class.getsimplename();     private iwebservicetaskhelper helper;     private iccpresponseparser<getsimulationstatusresponse> responseparser =              new ccpresponseparser<getsimulationstatusresponse>();       public getsimulationstatusasynctask(final iwebservicetaskhelper ahelper) {         helper = ahelper;     }      @override     public void addnamevaluepair(final string aname, final string avalue) {         helper.addnamevaluepair(aname, avalue);     }      @override     protected getsimulationstatusresponse doinbackground(string... aargs) {         return (getsimulationstatusresponse)utils.processresponse(this.helper, tag, responseparser,                  getsimulationstatusresponse.class, aargs);     }      @override     public void sendrequest(final string[] aargs) {         execute(aargs);     } } 

java code:

@test public void test() throws jsonprocessingexception, malformedurlexception {     final getuseridrequest request = new getuseridrequest();      request.setdeviceid("pc1");      final string requestasstring = converttojson(request);     final string serverurl = getserverurl("getuseridservice");      final url url = new url(serverurl);     httpurlconnection connection = null;     inputstream inputstream = null;     try {          connection = (httpurlconnection) url.openconnection();         connection.addrequestproperty("request", requestasstring);         connection.connect();          inputstream = connection.getinputstream();         final string responseasstring = ioutils.tostring(inputstream);          logger.debug("responseasstring: " + responseasstring);      } catch (final ioexception exception) {         logger.error("", exception);     }         {         ioutils.closequietly(inputstream);     } }  private string converttojson(final getuseridrequest arequest) throws jsonprocessingexception {     final objectmapper mapper = new objectmapper();     return mapper.writevalueasstring(arequest); }  private string getserverurl(final string aservicename) {     return "http://myserver.com/myapp/rest/" + aservicename; } 

update 1 (09.10.2013 12:23 msk):

@path("/getsimulationstatusservice") public class getsimulationstatusservice extends baseccpservice  {   private getsimulationstatusrequestparser requestparser =        new getsimulationstatusrequestparser();    @post   @produces("text/plain")   public string getsimulationstatus(@formparam("request") final string arequestjson)        throws jsonprocessingexception   {     final getsimulationstatusrequest request = requestparser.convert(arequestjson);      final getsimulationstatusresponse response = new getsimulationstatusresponse();      response.setrequestid(request.getid());     response.setcycle(getpersistence().getcurrentcycle(request.getuserid()));     response.setlabourforce(getpersistence().getlabourforcesimulationparameter(         request.getuserid()));      return getobjectmapper().writevalueasstring(response);   } } 

update 2 (09.10.2013 20:48 msk): when change code shown below, 500 http response. @ server side, arequest argument of method getuseridservice.getuserid null.

        connection = (httpurlconnection) url.openconnection();         connection.addrequestproperty("request", requestasstring);         connection.setrequestmethod("post"); // added line         connection.connect(); 

update 3 (09.10.2013 23:15): 1 works:

@test public void test() throws jsonprocessingexception, malformedurlexception  {     final getuseridrequest request = new getuseridrequest();      request.setdeviceid("pc1");      final string requestasstring = converttojson(request);     final string serverurl = getserverurl("getuseridservice");      final url url = new url(serverurl);     httpurlconnection connection = null;     inputstream inputstream = null;     outputstream outputstream = null;     try {          connection = (httpurlconnection) url.openconnection();         connection.setrequestmethod("post");         connection.setdooutput(true);         connection.connect();          outputstream = connection.getoutputstream();          ioutils.write("request=" + requestasstring, outputstream);          inputstream = connection.getinputstream();         final string responseasstring = ioutils.tostring(inputstream);          logger.debug("responseasstring: " + responseasstring);      } catch (final ioexception exception) {         logger.error("", exception);     }         {         ioutils.closequietly(inputstream);         ioutils.closequietly(outputstream);     } } 

the 405 http error code means given method (get) not supported endpoint. instead of request want send post. don't know kind of request sent android client.

do have endpoint specification/documentation?

here you'll find information how invoke post using plain java api. if can use external libraries in test can achieved lot easier using resteasy.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -