Returning an array of objects to javascript via JsInterop - how?

66 views
Skip to first unread message

Power Droid

unread,
Jan 2, 2018, 4:28:34 PM1/2/18
to GWT Users
I have a class which is just a data storage object

public class Product{ 
int id;  
String name;
public Product()
{
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}  
 
}  

I have another class that will be used to process data and will have a method to return an array of Product objects based on the processing.

public class Processor {
public Product[] doStuff()
{
  ... 
}
}

My goal is to use JsInterop to make the doStuff() method available to other javascript on a site.

Is this as simple as putting the @JsType annotation at the top of both of these classes?  If so, does GWT handle the conversion of the array of Java Product objects into an array of javascript Product objects or is there something else that needs to be done?

Colin Alworth

unread,
Jan 3, 2018, 3:00:13 PM1/3/18
to GWT Users
Yes, that should work - a "java array" of js objects can be a js array. You could also use elemental2.core.JsArray<Product> instead if you wanted to be explicit in your Java code that you are dealing with a Java array.
Reply all
Reply to author
Forward
0 new messages