Java 从浏览器访问 Web 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2235050/
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
Accessing a web service from your browser
提问by Steve
I'm relatively new to how web services work so I've gone though a tutorial from http://blog.saminda.org/2008/07/light-weight-osgi-based-axis2-powered.htmlwhich sets up a web service that prints hello.
我对 Web 服务的工作方式比较陌生,所以我已经阅读了http://blog.saminda.org/2008/07/light-weight-osgi-based-axis2-powered.html中的教程,该教程 设置了一个打印 hello 的 Web 服务。
The code to print out hello is here. In the same project here, there is another web service that adds two numbers together.
打印 hello 的代码在这里。在同一个项目在这里,有一个两数相加在一起,另一个Web服务。
To access the "hello" web service, I just go to my browser and go to http://localhost:8080/bridge/services/Version/getVersion.
要访问“hello”Web 服务,我只需转到我的浏览器并转到http://localhost:8080/bridge/services/Version/getVersion。
But how do I do that for the calculator web service? What's the url? Or do I have to do something extra to register that as a service first?
但是我如何为计算器网络服务做到这一点?网址是什么?还是我必须先做一些额外的事情才能将其注册为服务?
采纳答案by Rob Cowell
Looks like it would be http://localhost:8080/bridge/services/Calculator/add
看起来应该是http://localhost:8080/bridge/services/Calculator/add
It seems to expect to parameters but there's no indication of how to pass them in the url - you could try appending ?x=2&y=3 to it and see if it returns 5
它似乎期望参数,但没有指示如何在 url 中传递它们 - 您可以尝试将 ?x=2&y=3 附加到它,看看它是否返回 5
UPDATE: Yup that seems right, see the post at http://blog.saminda.org/2008/08/exposing-osgi-service-as-web-service.htmlfor a calculator example
更新:是的,这似乎是正确的,有关计算器示例,请参阅http://blog.saminda.org/2008/08/exposing-osgi-service-as-web-service.html 上的帖子
UPDATE 2: As I've commented on the original question, the links to the code which you are calling are no longer available. However, it seems like there is a general principle to calling this things. The format seems to be
更新 2:正如我对原始问题的评论,指向您调用的代码的链接不再可用。然而,似乎有一个普遍的原则来称呼这些东西。格式好像是
http://server:port/bridge/services/ClassName/MethodName?parameter=value&anotherparam=value
http://server:port/bridge/services/ClassName/MethodName?parameter=value&anotherparam=value
so in the adding numbers example, ClassName is Calculator, MethodName is add and the x and y parameters (that the code adds together) are passed as URL parameters.
所以在添加数字的例子中,ClassName 是 Calculator,MethodName 是 add,x 和 y 参数(代码相加)作为 URL 参数传递。
I'm not a Java developer myself, so I don't know if you need to compile the web service code first before it is callable, but if you've got the "hello" code working already, you'll know the answer to this.
我自己不是 Java 开发人员,所以我不知道您是否需要在可调用之前先编译 Web 服务代码,但是如果您已经使用了“hello”代码,您就会知道答案对此。
回答by mythz
Well if you have just starting out developing with web-services then you should look at servicestack.netit's an open source web service framework that reduces the learning curve in creating and consuming web services, here is a 'live example' showing you all the source code required to create and call simple web services from an Ajax, Android iOS, or Silverlight applications:
好吧,如果您刚刚开始使用 Web 服务进行开发,那么您应该查看servicestack.net,它是一个开源 Web 服务框架,可以减少创建和使用 Web 服务的学习曲线,这里是一个“实时示例”,向您展示了所有从 Ajax、Android iOS 或 Silverlight 应用程序创建和调用简单 Web 服务所需的源代码:
回答by Padmarag
I think it'll be - http://localhost:8080/bridge/services/Calculator/add?x=10&y=10
If you are starting with Java Web Services, do read the book "SOA Using Java Web Services", for details see http://www.soabook.com
我认为它会是 - http://localhost:8080/bridge/services/Calculator/add?x=10&y=10
如果您开始使用 Java Web 服务,请阅读“使用 Java Web 服务的 SOA”一书,对于详情见http://www.soabook.com