Monday, October 25, 2010

Scala, Jax-RS, and GoGrid: Trouble with annotations


At the moment, I am trying to create a scala client that talks to GoGrid using their REST-like API.  I am using Apache's Jax-RS implementation.  There are examples (here and here) available online on how to use the Apache implementation to create a Java client and I have been trying to adapt these examples to Scala.

One annoyance I have encountered so far is how to convert:
@Path("/myResource")
@Produces("text/plain")
public class SomeResource {
    @GET
    public String doGetAsPlainText() {
        ...
    }
}

My first pass at this is:
import javax.ws.rs.{ Path, GET, Produces }

@Path("grid/server/list")
@Produces("text/plain")
class ListRequest {
    @GET
    var plainText : String = _
}

Which produces an obscure error (or at least obscure to me):
Error: "annotation argument needs to be a constant; found: "text/plain" {<error>}
After trying a variety of things to make this acceptable for Scala and a lot of time searching to no avail, I randomly came across this post, where the author is trying to do something similar and found out that the code needs to look like this:
import javax.ws.rs.{ Path, GET, Produces }

@Path("grid/server/list")
@Produces(Array("text/plain"))
class ListRequest {
    @GET
    var plainText : String = _
}

Note that "text/plain" is now an element of an array...so apparently when the error says that the argument needs to be a constant...it meant it needs to be in an Array.  Okay...

No comments:

Post a Comment