简单的Java Web服务 [英] Simple Java web services

查看:56
本文介绍了简单的Java Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

方案:我想从本地LAN上的其他计算机访问服务类型应用程序中的一组Java方法.

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.

推荐答案

好吧,Tomcat或Jetty对于将某些方法发布为Web服务可能是过大的.但另一方面,它并不太复杂,他们可以完成工作,所以为什么不这样做呢?

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?

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

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:

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

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>

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

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.

阅读 http://ws.apache.org/axis2/1_4_1/contents.html 了解有关使用Axis2的更多信息.在文档中找不到与我描述的方法完全不同的方法,但是效果很好.

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.

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

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)

这篇关于简单的Java Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆