In Emerick, Carper & Grand's 2012 O'Reilly book, Clojure Programming, they give an example of using Java interop to create JAX-RS services using a Grizzly server. These examples are now outdated and don't work with recent versions of Jersey.
Here's an updated version that is working correctly, at least for this tiny example.
jaxrs_application.clj
(ns cemerick-cp.jaxrs-application
(:gen-class :name cemerick_cp.MyApplication
:extends javax.ws.rs.core.Application)
(:import [java.util HashSet])
(:require [cemerick-cp.jaxrs-annotations]))
(defn- -getClasses [this]
(doto (HashSet.)
(.add cemerick_cp.jaxrs_annotations.GreetingResource)))
jaxrs_annotations.clj
(ns cemerick-cp.jaxrs-annotations
(:import [javax.ws.rs Path PathParam Produces GET]))
(definterface Greeting
(greet [^String vistor-name]))
(deftype ^{Path "/greet/{visitorname}"} GreetingResource []
Greeting
(^{GET true Produces ["text/plain"]} greet ; annotated method
[this ^{PathParam "visitorname"} visitor-name] ; annotated method argument
(format "Hello, %s!" visitor-name)))
jaxrs_server.clj
(ns cemerick-cp.jaxrs-server
(:import [org.glassfish.jersey.grizzly2.servlet GrizzlyWebContainerFactory]))
(def myserver (atom nil))
(def properties
{"javax.ws.rs.Application" "cemerick_cp.MyApplication"})
(defn start-server []
(reset! myserver (GrizzlyWebContainerFactory/create "http://localhost:8080/"
properties)))
This uses the following Leiningen coordinates to run.
[org.glassfish.jersey.containers/jersey-container-grizzly2-servlet "2.26"]
[org.glassfish.jersey.inject/jersey-hk2 "2.26"]]
You probably also need to AOT some of these namespaces, I used :aot :all
for
this example.