[akka-user] How to annotate a POSO with JAXB annotations

309 views
Skip to first unread message

Shimi

unread,
Apr 20, 2010, 10:48:30 AM4/20/10
to Akka User List
I have some Classes written in Scala which I use In a Java Akka
project with REST service.
I want to return the object as JSON/XML but I am not sure where to put
the annotations in the class.

I tried this
class Name(@XmlElement(name="id") val id: String,
@XmlElementWrapper(name="links")
@XmlElement(name="link") val links: List[Link])

But I get
javax.xml.bind.JAXBException: class com.shimi.example.Name nor any of
its super class is known to this context

Any idea?

Shimi

--
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To post to this group, send email to akka...@googlegroups.com.
To unsubscribe from this group, send email to akka-user+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/akka-user?hl=en.

Shimi

unread,
Apr 20, 2010, 11:36:28 AM4/20/10
to Akka User List
My POSO extends a trait and It is a member of a POJO which is returned
by the REST service.
I changed the type in the POJO from the trait name to the POSO name
and now I get another error:
@XmlElementWrapper is only allowed on a collection property but
"com.shimi.example.Name.links" is not a collection property.
It doesn't count scala.collection.immutable.List as a collection.

Does anyone know how I can use scala.collection.immutable.List with
JAXB?

Shimi

Viktor Klang

unread,
Apr 20, 2010, 12:25:31 PM4/20/10
to akka...@googlegroups.com
On Tue, Apr 20, 2010 at 5:36 PM, Shimi <shi...@gmail.com> wrote:
My POSO extends a trait and It is a member of a POJO which is returned
by the REST service.
I changed the type in the POJO from the trait name to the POSO name
and now I get another error:
@XmlElementWrapper is only allowed on a collection property but
"com.shimi.example.Name.links" is not a collection property.
It doesn't count scala.collection.immutable.List as a collection.

Does anyone know how I can use scala.collection.immutable.List with
JAXB?

you _could_ try to do something like:

import scala.collection.JavaConversions._
import java.util.{List => JList,Arrays}

class Foo(var children : List[Foo]){
   @ANNOTATIONS_GO_HERE def getChildren() : JList[Foo] = Arrays.asList(children:_*)
   def setChildren(children : JList[Foo]) = this.children = children.toList
}
 
Cleaner solutions are appreciated.

Cheers,




--
Viktor Klang
| "A complex system that works is invariably
| found to have evolved from a simple system
| that worked." - John Gall

Akka - the Actor Kernel: Akkasource.org
Twttr: twitter.com/viktorklang

Shimi

unread,
Apr 20, 2010, 1:11:39 PM4/20/10
to Akka User List
Problem solved.
Thanks

On Apr 20, 7:25 pm, Viktor Klang <viktor.kl...@gmail.com> wrote:
> > akka-user+...@googlegroups.com<akka-user%2Bunsubscribe@googlegroups .com>
> > .
> > > For more options, visit this group athttp://
> > groups.google.com/group/akka-user?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Akka User List" group.
> > To post to this group, send email to akka...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > akka-user+...@googlegroups.com<akka-user%2Bunsubscribe@googlegroups .com>
> > .

atdestoryer

unread,
Apr 20, 2010, 2:59:31 PM4/20/10
to Akka User List
It looks like you have your problem solved but I thought I post this
anyway.

I have a similar project that uses Lift and exposes a REST api. I used
the protobuf-java-format project (http://protobuf-java-
format.googlecode.com) to provide easy serialization between json/java
etc. It was easier than JAXB to use and it provided more by provided
serialization to/from proto, json, xml and browser readable html.

2 cents..

-Andy

Shimi

unread,
Apr 21, 2010, 2:01:50 PM4/21/10
to Akka User List
How do you use it together with Jeresy?

atdestoryer

unread,
Apr 21, 2010, 3:11:03 PM4/21/10
to Akka User List
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())
Reply all
Reply to author
Forward
0 new messages