Earthly Powers
- All
- Fast Infoset
- General
- Java
- REST
More scala with Jersey
The following Scala class exercises just about every annotation-based mechanism currently supported by Jersey:
import javax.ws.rs.{UriTemplate, HttpMethod, ProduceMime, UriParam}
import javax.ws.rs.core.{HttpContext, UriInfo, HttpHeaders}
@UriTemplate("/{id}")
class MyResource {
@HttpContext var uriInfo: UriInfo = _
@HttpContext var headers: HttpHeaders = _
@ProduceMime(Array("text/html"))
@HttpMethod
def getMe(@UriParam("id") id: String) = <html>
<body>
<p>ID: { id }</p>
<p>URI : { uriInfo.getURI() }</p>
</body>
</html>.toString()
@HttpMethod
def postMe(@UriParam("id") id: String,
entity: String) : String = <xhtml>
<body>
<p>POSTed entity: { entity }</p>
<p>Content-Type: { headers.getMediaType() }</p>
</body>
</xhtml>.toString()
@UriTemplate("sub-method")
@HttpMethod
@ProduceMime(Array("text/plain"))
def getSubMe = "sub-method"
@UriTemplate("sub-class")
def getSubClassResource = new SubClassResource
@ProduceMime(Array("text/plain"))
class SubClassResource {
@HttpMethod
def getSubMe = "sub-class resource"
}
@UriTemplate("sub-object")
def getSubObjectResource = SubObjectResource
@ProduceMime(Array("text/plain"))
object SubObjectResource {
@HttpMethod
def getSubMe = "sub-object resource"
}
}
The UriInfo and HttpHeaders are injected into fields of the MyResource class. The template parameter value for the template parameter {id}, using the UriParam annotation, is injected as parameter of the getMe and postMe methods. Sub-methods (annotated with UriTemplate and HttpMethod) work fine, as do sub-locator methods (annotated with UriTemplate) getSubClassResource and getSubObjectResource that return a new class or an object, respectively, to continue the URI matching process.
The use of the XML syntax is nice (although i wish there were a way to switch off mixed content i.e. ignoring the white space for indenting). I suspect that it is possible to develop an entity provider for scala.xml.Elem class to convert to a String and to also strip out indenting white space.
Posted at 04:20PM Jul 05, 2007 by Paul Sandoz in REST | Comments[0]