fakod / sjersey   0.4.3

GitHub

Scala sugar for Jersey (JAX-RS Reference Implementation)

Scala versions: 2.11 2.10

SJersey

Life's too short to use java.util.Collection

SJersey is a set of classes which add Scala interoperation to Jersey. It is intended to be a replacement for jersey-scala. Instead of Jerkson, jackson-module-scala (Jackson JSON) is used now.

##Versions:

sjersey_2.10 & sjersey_2.11 0.4.3

  • Build for both Scala 2.10 and 2.11.
  • Bumped Jersey to 2.21
  • Bumped jackson-module-scala to 2.5.1
  • Added extractor support for @HeaderParam and @FormParam
  • thanks to @kelnos for this

sjersey_2.10 0.4.1

  • Removed unneeded code from trait JacksonDeAndSerializer
  • Fix: a WAR was deployed to Maven Central instead of the JAR

sjersey_2.10 0.4.0

  • bumped Jersey Version to 2.5.1
  • bumped jackson-module-scala to 2.3.1

sjersey_2.10 0.3.3

  • bumped to Scala 2.10.3 and deployed to Central
  • bumped versions for jersey-server and jackson-module-scala

How To Use

First, specify Jersey-Scala as a dependency:

<dependency>
    <groupId>eu.fakod</groupId>
    <artifactId>sjersey_${scala.version.short}</artifactId>
    <version>0.4.2</version>
</dependency>

Currently valid values for ${scala.verion.short} are 2.10 and 2.11.

Second, configure the servlet (in case of using one):

<servlet>
    <servlet-name>sjersey-service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>

    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>eu.fakod.sjersey.util.RegisterParameterInjectionBinder</param-value>
    </init-param>

    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>my.resources.package;eu.fakod.sjersey.providers</param-value>
    </init-param>
</servlet>

Third, write your resource classes:

case class FooCC(s: String, i: Int, d: Double, b: Boolean)
case class BarCC(s: String, i: Int)

@Path("/things")
@Consumes(Array("application/json"))
@Produces(Array("application/json"))
class Things {
  @GET
  def getAThing(@QueryParam("name") names: Set[String]) = "I found: " + names.mkString(", ")
  
  @POST
  def postAThing(cc: FooCC) = BarCC("received", cc.i)
}

What All This Supports (subject to change, resp. not all is validated)

  • QueryParam-, HeaderParam-, and FormParam-annotated parameters of type Seq[String], List[String], Vector[String], IndexedSeq[String], Set[String], and Option[String].
  • JsonNode request and response entities.
  • Case class (i.e., Product instances) JSON request and response entities.
  • Array[A] request and response entities. (Due to the JVM's type erasure and mismatches between Scala and Java type signatures, this is the only "generic" class supported since Array type parameters are reified.)