Skip to content

Instantly share code, notes, and snippets.

@rjungemann
Forked from headius/1. restful_service.rb
Created June 2, 2010 01:51
Show Gist options
  • Save rjungemann/421805 to your computer and use it in GitHub Desktop.
Save rjungemann/421805 to your computer and use it in GitHub Desktop.
Sinatra-like example in JRuby with Jersey
require 'java'
java_import 'javax.ws.rs.Path'
java_import 'javax.ws.rs.GET'
java_import 'javax.ws.rs.Produces'
java_package 'com.headius.demo.jersey'
java_annotation 'Path("/helloworld")'
class HelloWorld
java_annotation 'GET'
java_annotation 'Produces("text/plain")'
def cliched_message
"Hello World"
end
end
require 'java'
java_import com.sun.jersey.api.container.grizzly.GrizzlyWebContainerFactory
base_uri = "http://localhost:9998/"
init_params = {
"com.sun.jersey.config.property.packages" => "com.headius.demo.jersey"
}
puts "Starting grizzly"
thread_selector = GrizzlyWebContainerFactory.create(
base_uri, init_params.to_java)
puts <<EOS
Jersey app started with WADL available at #{base_uri}application.wadl
Try out #{base_uri}helloworld
Hit enter to stop it...
EOS
gets
thread_selector.stop_endpoint
exit(0)
~/projects/jruby ➔ jrubyc -c ../jersey-archive-1.2/lib/jsr311-api-1.1.1.jar --javac restful_service.rb
Generating Java class HelloWorld to /Users/headius/projects/jruby/com/headius/demo/jersey/HelloWorld.java
javac -d /Users/headius/projects/jruby -cp /Users/headius/projects/jruby/lib/jruby.jar:../jersey-archive-1.2/lib/jsr311-api-1.1.1.jar /Users/headius/projects/jruby/com/headius/demo/jersey/HelloWorld.java
~/projects/jruby ➔ CLASSPATH=../jersey-archive-1.2/lib/jersey-core-1.2.jar:../jersey-archive-1.2/lib/jersey-server-1.2.jar:~/Downloads/grizzly-servlet-webserver-1.9.9.jar:../jersey-archive-1.2/lib/jsr311-api-1.1.1.jar:../jersey-archive-1.2/lib/asm-3.1.jar jruby main.rb
Starting grizzly
Jersey app started with WADL available at http://localhost:9998/application.wadl
Try out http://localhost:9998/helloworld
Hit enter to stop it...
~ ➔ curl http://localhost:9998/helloworld
Hello World
~ ➔
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment