Search code examples
scalablazeds

BlazeDS is not converting Scala classes to AMF


I'm new to Scala and BlazeDS. I am trying to write a very simple application where flex would call a method called getBook and the Scala service returns a Book object. There's no database involved. All I am doing is that I'm creating a new Instance of Book and returning it.

My problem is that I don't get a valid Book object on Flex as response. I'm including the code here.

Scala Book (scalaDemo.vo.Book.scala)

package scalaDemo.vo

case class Book (id:Long,name:String,authors:String) 

Scala Service (scalaDemo.GreetingService.scala)

    package scalaDemo
    import scalaDemo.vo.Book

    class GreetingService {

      def sayHello = "Hello, World!"

      def getBook (name:String):Book = new Book (10,name,"author")
    }

Flex Side Book (vo.Book.as)

package vo
{
        [RemoteClass(alias="scalaDemo.vo.Book")]
        public class Book
        {
                public var id:Number;
                public var name:String;
                public var authors:String;
        }
}

GreetingService.as

package services
{
    import mx.rpc.AsyncResponder;
    import mx.rpc.AsyncToken;
    import mx.rpc.remoting.RemoteObject;

    public class GreetingService
    {
        protected var ro : RemoteObject = new RemoteObject ("greetingService");

        public function sayHello (responder:AsyncResponder) : void {
            var token : AsyncToken = ro.sayHello();

            token.addResponder(responder);
        }

        public function getBook (name:String,responder:AsyncResponder) : void {
            var token : AsyncToken = ro.getBook(name);

            token.addResponder(responder);
        }

    }
}

remoting-config.xml

<destination id="greetingService">
    <properties>
        <source>scalaDemo.GreetingService</source>
        <scope>application</scope>
    </properties>
</destination>

I have placed the scala classes under tomcat/webapps/blazeds/WEB-INF/classes (I am using blazeds turnkey server)

Please help me.


Solution

  • Ok. This is where I went wrong. I'm sorry BlazeDS. I blamed you for nothing.

    This was how I defined my Book Class in Scala (the wrong way)

    package scalaDemo.vo
    case class Book (id:Long,name:String,authors:String) 
    

    For a Scala object to be serialized by BlazeDS we need to make sure of 2 things.

    • Each attribute of the class should be preceded by the Annotation @BeanProperty
    • Each attribute of the class should be declared as var

    Here is the correct definition of scalaDemo.Book that I mentioned in my question.

    package scalaDemo.vo
    import scala.reflect.BeanProperty
    
    case class Book (
        @BeanProperty
        var id:Long,
        @BeanProperty
        var name:String,
        @BeanProperty
        var authors:String) 
    

    Daniel C. Sorbal thank you for you interest. Now my next task would be to get Hibernate talking to Scala.