How can I send Java object between client and serv

2019-05-09 21:37发布

问题:

I have a client and a server both written in Java and sharing Java classes that should be sent between each other. I'm not sure which libraries I can use for this on mobile because I don't know what Dalvik supports, what RoboVM supports etc. Not sure what Gluon Mobile can do for me in this case.

Specifically I have a file that looks like this:

class Data {
    IntegerProperty int1 = new SimpleIntegerProperty(4);
    ObjectProperty<Person> person = new SimpleObjectProperty();
    ObservableList<Contact> contacts = FXCollections.observableArrayList();
    // other properties
    // also add the getters for the properties and the getters and setters for the values
}

Person and Contact are similar to the above - they contain mostly data properties and some methods for adding and removing from internal (private) lists etc. Basically they are like beans or POJOs only with properties wrappers. This is the data that needs to be sent between the server and the client but only the wrapped values are important - not the bindings. This leads me to the point about serialization: javaFX properties are not serializeable so it was suggested here to make such class as the above externalizeable and write and read the wrapped values.

Ultimately I don't care if i need to do this custom stuff (though it's a lot of work) or if there's a way around it. I need a method on the server like Data receiveDatafor(...) that the client can call, the server fetches the Data data and returns it. The client and server each have their own unrelated bindings to the Data object.

Currently we use RMI internally for desktop. I read that RMI isn't supported and it might not be that great of an option anyway but it does allow to just send java objects really easily. JavaEE has websockets which can transfer the binary form of the objects but it's JavaEE so I guess not supported. I'm not against JSONing and sending as Text but it seems more work than to just serialize - could be wrong. The communication method should support encryption for example when sending passwords. What are my options?

回答1:

In terms of client-server communication you can have a look at Gluon Connect and Gluon CloudLink.

Gluon Connect

An open source library:

Gluon Connect is a client-side library that simplifies binding your data from any source and format to your JavaFX UI controls. It works by retrieving data from a data source and converting that data from a specific format into JavaFX observable lists and observable objects that can be used directly in JavaFX UI controls.

It is also part of the Charm dependencies, so you have it already included when you create a new Gluon project.

See the documentation on how to use to create a FileProvider or a RestProvider, and also the GluonConnectRestProvider sample.

As the doc already mentions: with a RestClient you can "convert" a REST endpoint into an ObservableList:

// create a RestClient to the specific URL
RestClient restClient = RestClient.create()
    .requestMethod(RestClient.Method.GET)
    .host("https://api.stackexchange.com")
    .path("/2.2/errors");

// retrieve a list from the DataProvider
GluonObservableList<Error> errors = DataProvider
    .retrieveList(restClient.buildListDataReader(Error.class));

// create a JavaFX ListView and populate it with the retrieved list
ListView<Error> lvErrors = new ListView<>(errors);

The Notes sample uses Gluon Connect for local storage of Notes and Settings.

Note that these samples make uses of JavaFX POJOs (i.e. Error, Note and Settings use properties).

Gluon CloudLink

Gluon CloudLink enables enterprise and mobile developers to easily connect their different services and applications together, enabling bi-directional communications between mobile apps, enterprise infrastructure, and cloud systems.

The data is stored in the cloud, and you (as administrator) can access to it through a Dashboard.

See documentation about it here.

Have a look at the PWS-GluonCloudLink-Whiteboard sample: a full demo of a back-end application (webapp-mobile) running on the cloud (Pivotal Web Services) and a mobile client (mobile app).

In the client side, once you get a valid GluonClient, you can retrieve an observable list of items:

public GluonObservableList<Item> retrieveItems() {
    GluonObservableList<Item> items = DataProvider.retrieveList(gluonClient.createListDataReader("items", Item.class));
    return items;
}

As you can see, in the client you don't deal with REST endpoints, json serializing... Everything is just JavaFX observables. The connection with the backend is set in CloudLink with the Dashboard application, defining a REST Connector.