Simple Java web services

后端 未结 5 1418
孤城傲影
孤城傲影 2021-01-03 14:35

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

相关标签:
5条回答
  • 2021-01-03 14:57

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

    0 讨论(0)
  • 2021-01-03 15:04

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

    0 讨论(0)
  • 2021-01-03 15:09

    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)

    0 讨论(0)
  • 2021-01-03 15:16

    Erhm. Why not just use RMI?

    0 讨论(0)
  • 2021-01-03 15:16

    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.

    0 讨论(0)
提交回复
热议问题