Hi,
Let me start out by saying that I have a limited knowledge of jersey
because I use SpringMVC when in java land and Lift when in scala land.
Here is a link to a talk I found useful in using protos and REST
together.
http://www.infoq.com/presentations/RESTful-Web-Services-Orbitz
Basically you need to create a method to format the response with the
correct content-type/length based on the Accepted header or url
extendsion etc.. Like I said I don't really know jersey here is an
example from a project in Lift. I hope it helps..
-Andy
object RestAPI {
def dispatch : LiftRules.DispatchPF = {
// match api/offer/{id}
case req @ Req("api" :: "offers" :: id :: Nil, _, GetRequest) =>
() => {
try {
val (msg, status) = getOffer(id) match {
case Some(offer) =>
(OfferResponse.newBuilder.setResponse(successResponse).setOffer(offer.toProto).build,
200)
case None => (OfferResponse.newBuilder.setResponse(
Response.newBuilder.setSuccess(false).setMessage("Unable
to deliver offer").addErrors("OfferDetail not found for id:" +
id)).build, 404)
}
encodeResponse(req, msg, status)
} catch {
case excp => Full(ForbiddenResponse())
}
}
case req @ Req("api" :: "offer_wall" :: id :: Nil, _, GetRequest)
=> () => {
val resp =
OfferWallResponse.newBuilder.setResponse(successResponse).setWall(Wall.getById(id).toProto).build
encodeResponse(req, resp, 200)
}
}
def getOffer(id : String) = DS.find[OfferDetail]
(classOf[OfferDetail], java.lang.Long.valueOf(id))
val successResponse = Response.newBuilder.setSuccess(true).build
/**
* encode the response according to extension and deliver with
appropriate http status code
*/
def encodeResponse(req: Req, om :Message, statusCode : Int) :
Box[LiftResponse] = {
val headers = ("X-Protobuf-Schema",
"com.msm.arthur.protos.OfferMessage.proto") :: Nil
req.path.suffix match {
case "json" =>
Full(JsonResponse(JsonParser.parse(JsonFormat.printToString(om)),
headers, Nil, statusCode))
case "proto" => Full(InMemoryResponse(om.toByteArray,
("Content-Length", om.toByteArray.length.toString) ::
("Content-Type", "application/x-protobuf") :: headers,
Nil, statusCode))
case "xml" =>
Full(XmlResponse(XML.loadString(XmlFormat.printToString(om)),
statusCode))
case "html" =>
Full(XhtmlResponse(XML.loadString(HtmlFormat.printToString(om)),
Full(DocType.xhtmlTransitional),("Content-Type", "text/html") ::
headers, Nil, statusCode, false))
case s => println(s); Full(NotFoundResponse())