简体   繁体   中英

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

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.

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> 

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM