java 如何通过单击链接调用 REST Web 服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27186393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:21:49  来源:igfitidea点击:

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

javarestjerseyjax-rs

提问by Subho

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

我对 REST 完全陌生。我试图通过研究这个链接来调用 webservice-

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

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.

它的效果很好,但在这里我需要在浏览器中写一个 url 然后它会显示输出。我希望在页面中会有一个按钮或链接 n,如果我点击它,那么 url 将点击浏览器并显示输出。有没有办法做到这一点?任何代码或链接,您可以提供任何帮助。

回答by Atilla Ozgur

You have two parts to your question.

你的问题有两个部分。

  • server side - Jax-RS Rest Service
  • client side - javascript
  • 服务器端 - Jax-RS 休息服务
  • 客户端 - javascript

See following code for javascript call of rest service:

有关休息服务的 javascript 调用,请参阅以下代码:

$.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>

回答by user1438038

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.

如果您想处理返回值(您当然会这样做),您将需要某种 JavaScript 库,例如 jQuery 来发出请求并获取返回的内容(通常是 JSON 代码)。例如,看看jQuery.getJSON()