Fork us on GitHub

New Rest Calls

Knowing how to stop and the way to stop is crucial
New Rest Calls

New Rest Calls

I really like the newer Rest API’s Chen added a while back. They are ultimately far more convenient than the ConnectionRequest API’s for most day to day development. In fact I used them almost exclusively in the Uber clone app. As a result of that I added a few useful enhancements and capabilities.

If you are not familiar with the API it works roughly like this:

Map<String, Object> jsonData = Rest.
            get(myUrl).
            acceptJson().
            getAsJsonMap();

This will request using an HTTP GET method from the url. It will set the accepts header to JSON and parse the response.

Content Type

An important ommission was the content type setting, you could use the header() value to achieve that but content type is an important header and deserves its own method:

Map<String, Object> jsonData = Rest.
            post(myUrl).
            contentType("application/json").
            body(bodyContentAsJSON).
            acceptJson().
            getAsJsonMap();

In this case we submit JSON data in a post request with the application/json content type.

jsonContent

Notice that there is duplicate data in the sample above where both acceptJson and the contentType effectively communicate the same idea of using JSON for the protocol (albeit the two sides of the protocol). Since this is a common case it can use the shorthand:

Map<String, Object> jsonData = Rest.
            post(myUrl).
            jsonContent().
            body(bodyContentAsJSON).
            getAsJsonMap();

Asynchronous Callbacks

We always had asynchronous callbacks in the API but in the past they were delivered via the interface Callback which has 2 methods. That means we couldn’t leverage the shorthand lambda notation when implementing simple requests. We added two new permutations to the method and removed the Async suffix for them to avoid potential collision.

E.g. in the past I would have had to write something like this:

Map<String, Object> jsonData = Rest.
            post(myUrl).
            jsonContent().
            body(bodyContentAsJSON).
            getAsJsonMapAsync(new Callback<Response<String>>() {
                @Override
                public void onSucess(Response<String> value) {
                    // code here...
                }

                @Override
                public void onError(Object sender, Throwable err, int errorCode, String errorMessage) {
                    // code here...
                }
        });

We can now use a terse approach like this:

Map<String, Object> jsonData = Rest.
            post(myUrl).
            jsonContent().
            body(bodyContentAsJSON).
            getAsJsonMap(value -> {
                    // code here...
            });

You will notice I somewhat glazed over the error handling which will go to the global error handler in this case. You can avoid that by overriding the error handler separately in the second argument to this method (which is optional).

Share this Post:

Posted by Shai Almog

Shai is the co-founder of Codename One. He's been a professional programmer for over 25 years. During that time he has worked with dozens of companies including Sun Microsystems.
For more follow Shai on Twitter & github.