0

I'm using BlazeDS in Tomcat7 and Flex. I'm trying to use custom classes between the client and server. In as:

package
{
   [Bindable]
   [RemoteClass(alias="remoting.Product")]
   public class Product 
   {
      public var name:String;
      public var id:int;
      public var isVisible:Boolean;
   }
}

In Java:

package remoting;
public class Product {

    public String name;
    public int id;
    public Boolean isVisible;

    public Product(){
            name = "Product 0.1";
            id = 123;
            isVisible = false;
    }
    public void setName(String _name){
            name = _name;
    }
    public void setId(int _id){
            id = _id;
    }
    public void setVisible(Boolean _isVisible){
            isVisible = _isVisible;
    }
}

Service part:

public Product echo() {
        Product product = new Product();
        product.setId(123);
        product.setName("My Product");
        product.setVisible(true);
        return product;
}

I can successfully set the destination of the RemoteObject and call the echo() method. The result event fires up, with the Product object in event.result. However, it does not contain any sensible data. The variables from AS class just get initialized with null, 0 and true values. I'm wondering what's the problem. I tried returning a String with parameters from Product and it works fine, so they get set fine. The problem is in mapping.

I could go another way and implement Externalizable but I don't understand this part from the example:

 name = (String)in.readObject();
 properties = (Map)in.readObject();
 price = in.readFloat();

What if there is a number of strings?

Cheers.

zavr
  • 2,049
  • 2
  • 18
  • 28
  • Do you create an instance of the Product class in your Flex application? If not; then the compiler will "optimize" that class out of the final SWF and the service result will turn into the generic Object class instead of your custom object class. – JeffryHouser Oct 06 '13 at 14:15
  • Yes, I do: var myProduct:Product = Product(event.result); – zavr Oct 06 '13 at 18:15

2 Answers2

1

In java class: use private fields and implement getters.

package remoting;
public class Product {

    private String name;
    private int id;
    private Boolean isVisible;

    public Product() {
            name = "Product 0.1";
            id = 123;
            isVisible = false;
    }
    public void setName(String _name){
            name = _name;
    }
    public String getName(){
            return name;
    }
    public void setId(int _id){
            id = _id;
    }
    public int getId(){
            return id;
    }
    public void setIsVisible(Boolean _isVisible){
            isVisible = _isVisible;
    }
    public Boolean getIsVisible() {
            return isVisible;
    }
}
Timofei Davydik
  • 7,244
  • 7
  • 33
  • 59
  • Dude you're the best! Thanks a lot. I assume that getters as function can have any name as long as they return a certain private variable? – zavr Oct 08 '13 at 13:28
  • I don't think so. They should be named following java bean rules – Timofei Davydik Oct 08 '13 at 15:10
  • @zavr it's my misprint. Getters and setters in java should be named according to public properties (or get/set) in Actionscript. You have public var isVisible:Boolean in AS, so it should be getIsVisible and setIsVisible in Java. You have no error because classes are matched. But in this case visible are not transferred correctly. I've edited my code – Timofei Davydik Oct 09 '13 at 09:16
0

You could also switch from BlazeDS to GraniteDS: the latter has a powerful transparent externalization mechanism as well as code generation tools that can really save your time (see documentation here).

  • thanks, but I've spent a long time setting up BlazeDS and the deadline is approaching :D – zavr Oct 08 '13 at 18:18