Samples

REST

The org.redfx.rest package makes it easy for developers to make REST calls to remote URI's, and to obtain JavaFX Object instances instead of having to parse the result.
Two approaches are supported:

  • Create a org.redfx.rest.RestRequest instance with a number of (optional) parameters, and call its start method.
  • Use the org.redfx.rest.RestService.getUri() or org.redfx.rest.RestService.postUri() methods with a number of parameters.

I am not sure which approach is prefered by JavaFX developers, hence both are available.

In both cases, a callback function is called with an Object, and the type of this Object is a local JavaFX classname.

Devoxx Rest interface

The example uses the Devoxx REST Api. We will show how to obtain the details of an event, by calling http://cfp.devoxx.com/rest/v1/events/1.
RedFX will parse the JSON-result of this call into a JavaFX Object, which type is defined in our application. The value object we use for this has the type restsample.DevoxxEvent :

 1 package restsample;
 2
 3 /**
 4  * @author johan
 5  */
 6
 7 public class DevoxxEvent {
 8
 9   public var id: Integer;
10   public var location: String;
11   public var name: String;
12   public var description: String;
13   public var enabled: Boolean;
14
15 }
RestRequest

Click here to view this sample in action

The RestRequest approach is similar to the use of HttpRequest, but it parses the result directly into a JavaFX object.
The code below parses the devoxx REST result, and shows the title and description of the event in the Scene:

 1 package restsample;
 2
 3 import javafx.stage.Stage;
 4 import javafx.scene.Scene;
 5 import javafx.scene.text.Text;
 6 import javafx.scene.text.Font;
 7 import org.redfx.client.rest.RestRequest;
 8 import javafx.scene.layout.VBox;
 9
10 /**
11  * @author johan
12  */
13 var singleEvent: DevoxxEvent;
14
15 var restRequest: RestRequest = RestRequest {
16           uri: "http://cfp.devoxx.com/rest/v1/events/1"
17           clazz: "restsample.DevoxxEvent"
18           onResult: onResult;
19         };
20
21 function onResult(obj: Object): Void {
22   singleEvent = obj as DevoxxEvent;
23 }
24
25 var stage: Stage = Stage {
26           title: "Devoxx Rest Demo"
27           scene: Scene {
28             width: 400, height: 280
29             content: [
30               VBox {
31                 content: [
32                   Text {
33                     font: Font {size: 24}, x: 10, y: 30
34                     content: bind singleEvent.name
35                   },
36                    Text {
37                     font: Font {size: 16}, x: 10,  y: 30
38                     content: bind singleEvent.description
39                   }
40                 ]
41               }
42             ]
43           }
44         }
45
46 function run() {
47   restRequest.start();
48   stage;
49 }
50
51

At line 15, a RestRequest is created, and in the run method, at line 47, the request is started.
Three parameters are passed to the RestRequest:

  • uri: the location of the URI;
  • clazz: the fully qualified name of the JavaFX class that will contain the parsed result
  • onResult: a callback function that will be called when the RestRequest has executed the request and parsed the result.

The callback function, defined at line 21, will cast the result to the singleEvent instance, and some of its content will be shown --- see lines 34 and 38.

RestService

The approach using the RestService functions instead of the RestRequest objects is not that different. Only the modified code is shown below:

 1 package restsample;
 2
 3 import javafx.stage.Stage;
 4 import javafx.scene.Scene;
 5 import javafx.scene.text.Text;
 6 import javafx.scene.text.Font;
 7 import org.redfx.client.rest.RestRequest;
 8 import javafx.scene.layout.VBox;
 9 import org.redfx.client.rest.RestService;
10
11 /**
12  * @author johan
13  */
14 var singleEvent: DevoxxEvent;
15
16
17 function onResult(uri: String, obj: Object): Void {
18   singleEvent = obj as DevoxxEvent;
19 }
20
21 var stage: Stage = Stage {
....
40         }
41
42 function run() {
43   RestService.getUri("http://cfp.devoxx.com/rest/v1/events/1", "restsample.DevoxxEvent", onResult);
44   stage;
45 }
46
47

In the run-method, at line 43, the RestService.getUri() method is called, and a number of arguments are passed in this method. The callback-function, onResult is now called with both the uri information and the resulting data.