简体   繁体   English

简单的Java Web服务

[英]Simple Java web services

Does anyone know of a really simple way of publishing Java methods as web services? 有谁知道将Java方法作为Web服务发布的一种非常简单的方法? I don't really want the overhead of using Tomcat or Jetty or any of the other container frameworks. 我真的不想要使用Tomcat或Jetty或任何其他容器框架的开销。

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. 场景:我在服务类型应用程序中有一组Java方法,我想从本地LAN上的其他机器访问。

Well, Tomcat or Jetty may be overkill for publishing just some methods as a web service. 好吧,Tomcat或Jetty可能因为发布一些Web方法的方法而过度。 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. 不久前我遇到过类似的问题,并将Tomcat与Axis2一起使用。 Just download Tomcat, unpack it, deploy the Axis2 WAR. 只需下载Tomcat,解压缩,部署Axis2 WAR。 To publish a webservice, there are several aproaches, the one I took is probably one of the easiest: 要发布一个Web服务,有几个方法,我采取的可能是最容易的一个:

Just build your application as usual and annotate the web service class and methods with the appropriate annotaions from javax.jws.*. 只需像往常一样构建应用程序,并使用javax.jws。*中的相应注释来注释Web服务类和方法。 Package everything into a jar. 把所有东西都装进罐子里。 Create a service.xml in the META-INF directory of your jar file and put this into it: 在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>

Rename the .jar to .aar and put it into the /webapps/axis2/WEB-INF/services/ directory. 将.jar重命名为.aar并将其放入/ webapps / axis2 / WEB-INF / services /目录。 Start tomcat and the service will be deployed. 启动tomcat,将部署该服务。 You can check if it is running by visiting the axis2 page ( http://localhost:8080/axis2/ ). 您可以通过访问axis2页面( 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. 您还可以在那里获取WSDL URL以连接到您的服务。

Read http://ws.apache.org/axis2/1_4_1/contents.html for more about using Axis2. 有关使用Axis2的更多信息,请阅读http://ws.apache.org/axis2/1_4_1/contents.html 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 (eg serving of plain old web pages, jsps or other stuff), you can also use the Axis2 standalone server. 更新:如果您只想提供Web服务并且实际上不需要Tomcat的任何其他功能(例如,提供普通旧网页,jsps或其他内容),您还可以使用Axis2独立服务器。 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) 我已经写了一个稍微详细的版本,可以在http://www.slashslash.de/lang/en/2008/10/java-webservices-mit-apache-tomcat-und-axis2/找到 (不要让URL中的德语激怒你,它是用英文写的)

Web services depend on HTTP. Web服务依赖于HTTP。 You might not want tomcat or Jetty. 您可能不需要tomcat或Jetty。 In that case, you have to implement HTTP yourself. 在这种情况下,您必须自己实现HTTP。

Erhm. Erhm。 Why not just use RMI ? 为什么不直接使用RMI

Jetty's pretty lightweight. Jetty非常轻巧。 Otherwise, I think XML-RPC is your only sensible option. 否则,我认为XML-RPC是你唯一明智的选择。

The simplier solution than the one that Simon has discribed, ist to use the tools that alrady do that. 比Simon所描述的更简单的解决方案是使用alrady这样做的工具。 If you use eclipse you could use http://ws.apache.org/axis2/tools/1_2/eclipse/servicearchiver-plugin.html 如果您使用eclipse,可以使用http://ws.apache.org/axis2/tools/1_2/eclipse/servicearchiver-plugin.html

to generate the aar file. 生成aar文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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