Search This Blog

Tuesday, 8 March 2011

Accessing a JAX-RS RESTful Web Service From JRuby

In this example we access our deployed JAX-RS Web Service from Weblogic 10.3.4. For such a setup see the previous blog entry below.

Deploying a RESTful Web Service From JDeveloper 11g to Weblogic 10.3.4

1. Download the ZIP of Jersey which contains all the JARS we need here.

zip of Jersey

2. The 2 client JAR files we need are as follows. Place them into an directory where you will run the JRuby script from.

  • jersey-client-1.5.jar
  • jersey-core-1.5.jar

3. Create a JRuby script called "jax-rs-weblogic.rb" with content as follows
require 'java'

require 'jersey-client-1.5.jar'
require 'jersey-core-1.5.jar'

java_import 'com.sun.jersey.api.client.Client'
java_import 'com.sun.jersey.api.client.WebResource'

class TestJAXRSResource

  def initialize
    @client = Client.create
    @uri1 = "http://wayne-p2.au.oracle.com:7003/jaxrs-demo/jersey/helloworld/sayHello"
    @uri2 = "http://wayne-p2.au.oracle.com:7003/jaxrs-demo/jersey/helloworld/sayHelloWithName"
  end

  #add getters and setters for all attrributes  
  attr_reader :client, :uri1, :uri2
  
  def invoke_simple_method_get()
    puts "** Simple GET request .... **"
    resource = @client.resource @uri1
    response = resource.get java.lang.String.java_class
    puts response
  end

  def invoke_simple_method_get_with_param (name)
    puts "** Simple GET request with Query Param **"
    resource = @client.resource @uri2
    response = resource.queryParam("name", name).get(java.lang.String.java_class)
    puts response
  end
    
end


print "Run at #{Time.now} using JRuby #{RUBY_VERSION}\n\n"

print "** FMW Weblogic 10.3.4 JAX-RS Web Service Invoke Test **\n\n"

test = TestJAXRSResource.new
test.invoke_simple_method_get
test.invoke_simple_method_get_with_param :pas

print "\nEnded at #{Time.now} \n"

4. Edit the @uri1 and @uri2 instance variables to point to your Weblogic 10.3.4 server details and or paths to the JAX-RS Web Service.

5. Run as follows.

> jrb jax-rs-weblogic.rb

OUTPUT

Run at Tue Mar 08 07:57:54 +1100 2011 using JRuby 1.8.7


** FMW Weblogic 10.3.4 JAX-RS Web Service Invoke Test **


** Simple GET request .... **
Hello World!
** Simple GET request with Query Param **
Hello PAS


Ended at Tue Mar 08 07:57:54 +1100 2011

No comments: