Skip to content

Instantly share code, notes, and snippets.

@cjmamo
Last active December 20, 2015 18:29
Show Gist options
  • Save cjmamo/6176537 to your computer and use it in GitHub Desktop.
Save cjmamo/6176537 to your computer and use it in GitHub Desktop.
JRuby CXF: A Gem for Creating SOAP Web Services
class HelloWorld
def say_hello(name)
return 'Hello ' + name
end
def give_age(age)
return 'Your age is ' + age.to_s
end
end
require 'jruby_cxf'
class HelloWorld
include CXF::WebServiceServlet
def say_hello(name)
return 'Hello ' + name
end
def give_age(age)
return 'Your age is ' + age.to_s
end
end
require 'jruby_cxf'
class HelloWorld
include CXF::WebServiceServlet
def say_hello(name)
return 'Hello ' + name
end
def give_age(age)
return 'Your age is ' + age.to_s
end
end
# Servlet path defaults to '/' if no argument is passed to the constructor
hello_world = HelloWorld.new('/hello-world')
server = org.eclipse.jetty.server.Server.new(8080)
contexts = org.eclipse.jetty.server.handler.ContextHandlerCollection.new
server.set_handler(contexts)
root_context = org.eclipse.jetty.servlet.ServletContextHandler.new(contexts, "/")
root_context.addServlet(org.eclipse.jetty.servlet.ServletHolder.new(hello_world), "/*")
server.start
require 'jruby_cxf'
class HelloWorld
include CXF::WebServiceServlet
expose :say_hello, {:expects => [{:name => :string}], :returns => :string}
expose :give_age, {:expects => [{:age => :int}], :returns => :string}
def say_hello(name)
return 'Hello ' + name
end
def give_age(age)
return 'Your age is ' + age.to_s
end
end
...
require 'jruby_cxf'
class HelloWorld
include CXF::WebServiceServlet
expose :say_hello, {:expects => [{:name => :string}], :returns => :string}
expose :give_age, {:expects => [{:age => :int}], :returns => :string}
service_name 'MyExample'
service_namespace 'http://opensourcesoftwareandme.blogspot.com'
def say_hello(name)
return 'Hello ' + name
end
def give_age(age)
return 'Your age is ' + age.to_s
end
end
...
require 'jruby_cxf'
class Animal
include CXF::ComplexType
member :kind, :string
namespace 'http://opensourcesoftwareandme.blogspot.com'
end
class Person
include CXF::ComplexType
member :name, :string
member :age, :int
member :pet, :Animal, :required => false
namespace 'http://opensourcesoftwareandme.blogspot.com'
end
class HelloWorld
include CXF::WebServiceServlet
expose :say_hello, {:expects => [{:person => :Person}], :returns => :string}
expose :give_age, {:expects => [{:person => :Person}], :returns => :string}
service_name 'MyExample'
service_namespace 'http://opensourcesoftwareandme.blogspot.com'
def say_hello(person)
return 'Hello ' + person.name
end
def give_age(person)
return 'Your age is ' + person.age.to_s
end
end
...
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://rubyobj/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="HelloWorld" targetNamespace="http://rubyobj/">
<wsdl:portType name="HelloWorldPortType"></wsdl:portType>
<wsdl:binding name="HelloWorldSoapBinding" type="tns:HelloWorldPortType">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
</wsdl:binding>
<wsdl:service name="HelloWorld">
<wsdl:port binding="tns:HelloWorldSoapBinding" name="HelloWorldPort">
<soap:address location="http://localhost:8080/hello-world"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://rubyobj/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="HelloWorld" targetNamespace="http://rubyobj/">
<wsdl:types>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://rubyobj/" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://rubyobj/">
<xsd:element name="give_age" type="tns:give_age"/>
<xsd:complexType name="give_age">
<xsd:sequence>
<xsd:element minOccurs="0" name="age" type="xsd:int"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="give_ageResponse" type="tns:give_ageResponse"/>
<xsd:complexType name="give_ageResponse">
<xsd:sequence>
<xsd:element minOccurs="0" name="return" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="say_hello" type="tns:say_hello"/>
<xsd:complexType name="say_hello">
<xsd:sequence>
<xsd:element minOccurs="0" name="name" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="say_helloResponse" type="tns:say_helloResponse"/>
<xsd:complexType name="say_helloResponse">
<xsd:sequence>
<xsd:element minOccurs="0" name="return" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
</wsdl:types>
<wsdl:message name="give_age">
<wsdl:part element="tns:give_age" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="say_hello">
<wsdl:part element="tns:say_hello" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="give_ageResponse">
<wsdl:part element="tns:give_ageResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="say_helloResponse">
<wsdl:part element="tns:say_helloResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:portType name="HelloWorldPortType">
<wsdl:operation name="give_age">
<wsdl:input message="tns:give_age" name="give_age"></wsdl:input>
<wsdl:output message="tns:give_ageResponse" name="give_ageResponse"></wsdl:output>
</wsdl:operation>
<wsdl:operation name="say_hello">
<wsdl:input message="tns:say_hello" name="say_hello"></wsdl:input>
<wsdl:output message="tns:say_helloResponse" name="say_helloResponse"></wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="HelloWorldSoapBinding" type="tns:HelloWorldPortType">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="give_age">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="give_age">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="give_ageResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="say_hello">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="say_hello">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="say_helloResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="HelloWorld">
<wsdl:port binding="tns:HelloWorldSoapBinding" name="HelloWorldPort">
<soap:address location="http://localhost:8080/hello-world"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
jruby -S gem install jruby-cxf
jruby -J-cp "/opt/apache-cxf-2.7.6/lib/*:/opt/jetty-distribution-8.1.12.v20130726/lib/*" example.rb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment