facebook

RESTFul web service tutorial

  1. MyEclipse Archived
  2.  > 
  3. Web Services
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #295760 Reply

    Douglas M Hurst
    Participant

    I completed the RESTful web service tutorial …

    Developing JAX-RS / REST Web Services

    … and it worked just fine… as far as it went. Is the intent that you communicate with RESTful WS’s with http requests from Javascript? Such as…

    Microsoft: xmlhttp=new ActiveXObject(“Microsoft.XMLHTTP”);
    Other browsers: xmlhttp=new XMLHttpRequest();

    ????

    With the JAX-WS tutorial, there was also a WS client that was built. Not so with this RESTful tutorial.

    In particular, I’m wondering how you would send an “add” request to the tutorial example. A listing of customers was…

    http://localhost:8080/services/customers

    …to list all customers and…

    http://localhost:8080/services/customers/<int&gt;

    … to list a specific customer. It appears from the annotations…

    @POST
    @Path(“add”)
    @Produces(“text/plain”)
    @Consumes(“application/xml”)

    …that it should be something like…

    http://localhost:8080/services/customers/add/&lt; customer xml>

    … an example of which seems like it ought to look like…

    http://localhost:8080/services/customers/add/<customer><name>Bill Adama</name><address>Vancouver, Canada</address></customer>

    … but I was not able to get this to work. I guess in all cases you need to have a listener in your Javascript to receive the output?

    Thanks

    #295817 Reply

    Brian Fernandes
    Moderator

    Douglas,

    With the JAX-WS tutorial, there was also a WS client that was built. Not so with this RESTful tutorial.

    We hear you, this is something we are looking at internally.

    As far as adding a customer, note that the method has a @POST annotation, it’s expecting the data as HTTP Post content, not in the URL itself.
    So you would send a request to “http://localhost:8080/services/customers/add&#8221;, and the Post data would be

    <customer><name>Bill Adama</name><address>Vancouver, Canada</address></customer>

    The above content should be used as a parameter the send method of your XMLHttpRequest object. This snippet should help:

    xmlhttp.setRequestHeader("Content-type", "application/xml");
    xmlhttp.send("<customer><name>Bill Adama</name><address>Vancouver, Canada</address></customer>");

    Hope this helps.

    #295849 Reply

    Douglas M Hurst
    Participant

    Yes it does, thank you very much.

    #296295 Reply

    Douglas M Hurst
    Participant

    That worked. Of course you’d want to set up a function to evaluate the response… which I didn’t do either.

    var objHTTP, strResult;
    var element = document.getElementById(‘inputId’);
    var inputXml = element.value;
    var myHttp = “http://localhost:8080/restdemo/services/customers/add&#8221;;
    objHTTP = new ActiveXObject(‘Microsoft.XMLHTTP’);
    objHTTP.open(‘POST’, myHttp, false);
    objHTTP.setRequestHeader(‘Content-Type’,’application/xml’); // request monitoring function here
    objHTTP.send(inputXml);

Viewing 4 posts - 1 through 4 (of 4 total)
Reply To: RESTFul web service tutorial

You must be logged in to post in the forum log in