简单的 Java Web 服务

发布于 2024-07-06 08:41:22 字数 141 浏览 4 评论 0原文

有谁知道将 Java 方法发布为 Web 服务的一种非常简单的方法吗? 我真的不想要使用 Tomcat 或 Jetty 或任何其他容器框架的开销。

场景:我在服务类型应用程序中有一组 Java 方法,我想从本地 LAN 上的其他计算机访问它们。

Does anyone know of a really simple way of publishing Java methods as web services? I don't really want the overhead of using Tomcat or Jetty or any of the other container frameworks.

Scenario: I've got a set of Java methods in a service type application that I want to access from other machines on the local LAN.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

北城孤痞 2024-07-13 08:41:22

好吧,Tomcat 或 Jetty 对于仅将某些方法发布为 Web 服务来说可能有点过分了。 但另一方面,它并不太复杂,而且他们可以完成这项工作,所以为什么不呢?

不久前我也遇到了类似的问题,并将 Tomcat 与 Axis2 一起使用。 只需下载 Tomcat,解压它,部署 Axis2 WAR。 要发布 Web 服务,有多种方法,我采用的方法可能是最简单的方法之一:

只需照常构建应用程序,并使用 javax.jws.* 中的适当注释来注释 Web 服务类和方法。 将所有东西打包到一个罐子里。 在 jar 文件的 META-INF 目录中创建 service.xml 并将其放入其中:

<service name="name of the service" scope="<one of request, session or application>">
    <description>
    optional description of your service
    </description>

    <messageReceivers>
        <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
        <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"  class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
    </messageReceivers>

    <parameter name="ServiceClass" locked="false">put here the fully qualified name of your service class (e.g. x.y.z.FooService)</parameter>

</service>

将 .jar 重命名为 .aar 并将其放入 /webapps/axis2/WEB-INF/services/ 目录中。 启动tomcat,服务就会被部署。 您可以通过访问 axis2 页面(http://localhost:8080/axis2/ 来检查它是否正在运行)。 在那里您将看到部署了哪些服务以及导出了哪些方法。 您还可以在那里获取 WSDL url 以连接到您的服务。

阅读http://ws.apache.org/axis2/1_4_1/contents.html 有关使用 Axis2 的更多信息。 我在这里描述的方法在文档中没有找到完全一样的方法,但它效果很好。

更新:如果您只是想提供 Web 服务,并且确实不需要 Tomcat 的任何其他功能(例如,提供普通旧网页、jsps 或其他内容),您还可以使用Axis2 独立服务器。 但除了设置部分之外,它不会改变我所描述的任何内容。

我写了一个稍微更详细的版本,可以在以下位置找到: http://www.slashslash.de/lang/en/2008/10/java-webservices-mit-apache-tomcat-und-axis2/ (不要让 URL 中的德语激怒您,它是用英语写的)

Well, Tomcat or Jetty may be overkill for publishing just some methods as a web service. But on the other hand its not too complicated and they do the job, so why not?

I had a similar problem not too long ago and used a Tomcat together with Axis2. Just download Tomcat, unpack it, deploy the Axis2 WAR. To publish a webservice, there are several aproaches, the one I took is probably one of the easiest:

Just build your application as usual and annotate the web service class and methods with the appropriate annotaions from javax.jws.*. Package everything into a jar. Create a service.xml in the META-INF directory of your jar file and put this into it:

<service name="name of the service" scope="<one of request, session or application>">
    <description>
    optional description of your service
    </description>

    <messageReceivers>
        <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
        <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"  class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
    </messageReceivers>

    <parameter name="ServiceClass" locked="false">put here the fully qualified name of your service class (e.g. x.y.z.FooService)</parameter>

</service>

Rename the .jar to .aar and put it into the /webapps/axis2/WEB-INF/services/ directory. Start tomcat and the service will be deployed. You can check if it is running by visiting the axis2 page (http://localhost:8080/axis2/). There you will see which services are deployed and which methods are exported. Also you can get the WSDL url there to connect to your service.

Read http://ws.apache.org/axis2/1_4_1/contents.html for more about using Axis2. The approach I described here is not found exactly like this in the docs, but it works very well.

Update: If you just want to provide web services and really don't need any of the other features of Tomcat (e.g. serving of plain old web pages, jsps or other stuff), you can also use the Axis2 standalone server. But except for the setup part it doesn't change anything I described.

I've written a slightly more detailed version of this, which can be found at: http://www.slashslash.de/lang/en/2008/10/java-webservices-mit-apache-tomcat-und-axis2/ (don't let the German in URL irritate you, it's written in English)

红ご颜醉 2024-07-13 08:41:22

Web 服务依赖于 HTTP。 您可能不需要 tomcat 或 Jetty。 在这种情况下,您必须自己实现 HTTP。

Web services depend on HTTP. You might not want tomcat or Jetty. In that case, you have to implement HTTP yourself.

一指流沙 2024-07-13 08:41:22

嗯。 为什么不直接使用 RMI 呢?

Erhm. Why not just use RMI?

漫雪独思 2024-07-13 08:41:22

Jetty 相当轻量。 否则,我认为 XML-RPC 是您唯一明智的选择。

Jetty's pretty lightweight. Otherwise, I think XML-RPC is your only sensible option.

爱你是孤单的心事 2024-07-13 08:41:22

比西蒙描述的解决方案更简单的解决方案是使用已经可以做到这一点的工具。 如果您使用 Eclipse,则可以使用 http://ws.apache .org/axis2/tools/1_2/eclipse/servicearchiver-plugin.html

生成 aar 文件。

The simplier solution than the one that Simon has discribed, ist to use the tools that alrady do that. If you use eclipse you could use http://ws.apache.org/axis2/tools/1_2/eclipse/servicearchiver-plugin.html

to generate the aar file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文