How to call REST web service with a click on a link

14,093

Solution 1

Well, triggering a request by clicking a link is fairly simple:

<a href="http://localhost:8080/JAXRS-HelloWorld/rest/helloWorldREST/JavaCodeGeeks?value=enjoy-REST">Request resource</a>

If you want to work with the return value (you certainly do), you will need some sort of JavaScript library, such as jQuery to issue requests and fetch the returned content (usually JSON code). Have a look at jQuery.getJSON(), for example.

Solution 2

You have two parts to your question.

  • server side - Jax-RS Rest Service
  • client side - javascript

See following code for javascript call of rest service:

$.get( "ajax/test.html", function( data ) {
  $( ".result" ).html( data );
  alert( "Load was performed." );
});

var restUrl = "https://api.stackexchange.com/2.2/info?site=stackoverflow"
$( document ).ready(function() {
  $.get(restUrl, function( data ) {
    alert( "total user " + data.items[0].total_users );
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Share:
14,093
Subho
Author by

Subho

Updated on June 18, 2022

Comments

  • Subho
    Subho almost 2 years

    I am totally new to REST. I was trying to call webservice by studying this link-

    http://examples.javacodegeeks.com/enterprise-java/rest/jersey/jersey-hello-world-example/

    Its works good but here I need to write an url in the browser then it'll show the output. I want that in a page there will be a button or a link n which if I click then the url will hit on the browser and output will show. Is there any way to do that? any codes or link whatever help you can please help.

    • Toumash
      Toumash over 9 years
      Can you use JavaScript? For example jQuerys $.ajax method? If not, you will have to cURL the service in php, and display the output