Creating an Android app for your website with Spring Android and REST

As Android is really hot at the moment (and I have an Android phone), I wanted to create an Android app for my website. In this blog post I will explain the changes I had to make on the part of my web application and how I used Spring Android to interact from Android to my web application.

Website

My website is just a simple Spring application with Spring MVC 3.0.5, JPA, Hibernate and MySQL. In order to create a REST api that is understandable for my Android app, I needed to add a little more configuration to the web application.

Spring Security

As I have Spring Security in place, I had to add a security rule for my REST api. If you really want to do it the right way, than make sure you use the OAuth implementation as Spring Android also provides you to use OAuth in your Android app.

But being a bit lazy I just configured the REST api to be public and therefor does not require any authentication. This is good for now.

As you can see in the code above, I have configured two mediaTypes. The json mediatype will be used by our Android app and the html mediatype will be used by any browser.

The defaultViews property allows us to override the views provided by the view resolvers. Which is necessary in order resolve the correct view for JSON.

Converting data

Later on this post I will show how you can POST data to your controllers via Spring Android. To do this we need to configure the MappingJacksonHttpMessageConverter that allows us to convert data of the media type application/json.

Controller

Now that we have the ContentNegotiatingViewResolver in place, we can call any URL in our web application with the .json extension and give you a JSON representation of your data. Let’s take a look at an example of a Controller method.

If I want to call this method via my browser, I can simple call /rest/matches/played.json. This will simply work for all your request mappings. In this example I convert the list of matches to a list of matchDto objects. These objects contain less data, so that I don’t send any unnecessary data to our Android app.

Well that’s basically it for the web application part. I have created separate controllers for my REST api, but you could easily reuse your existing controller if it contains all your data.

Android app

Spring Android

Spring Android provides us with a Rest Client and Commons Logging adapter for Android. I don’t want to spend any time on how to set-up an Android project, as everyone prefers their own IDE. But it is clear that we need the Spring Android jars in our project.

You can either download them from the Spring Android site of you can use Maven to include them into your project.

RestTemplate – GET

Retrieving data from our web application is really simple thanks to the RestTemplate.

The code above uses the getForObject() method to invoke the REST api from our web application and expects an object of the type Matches. This object contains an array of Match objects. The Match object contains all fields that are present in the JSON output.

You can just use the postForObject() method to POST any object to your web application. In this example I post a Credentials object which contains the username and password. This is not really secure, but fits it’s purpose for now.

@RequestBody and @ResponseBody

One thing that I want to mention is the use of @RequestBody and @ResponseBody in a controller in the web application part. In my web application I have the exact same Credentials object as in my Android app. This allows us to use the following controller code:

Because of the jsonConverter we configured earlier, the object in the request is mapped to the Credentials object in the web application.

With @ResponseBody I directly bind my method return value to the web response body. So there is no need for creating a model and as you could see in the corresponding Android part I can just expect a boolean to be returned.

Next time

Now that we are able to communicate from an Android app to a web application and vice versa, it is time to create a great Android app. In my next blog post I will explain how I applied a few layout patterns and Android how I tackled a few of my problems during the development of my Android app.

but I am wonder if there is a way in which I can use the RESTemplate to convert my java object at the android side and build a valid post request without need of the converter on the server, for getForEntity request it is working fine

Hi, i m new for Android. i am working in java/j2EE, I have seen there is spring support in Android at client side(Message Convertor in xml and json,SQLite) , but i am not getting any good resource, So If you have any resource or sample code , can you please send me on my gmail id?

After Registration spring or android client send username and passowrd to rest application through restservice url and in back rest spring application validate against the registered username and password from datasource and send back authorization. How to achieve this?

Busines Services

Spring or Android client send request to Restful spring application to get business services to display some data.

Where should i start ?. How to design this or any sample stuff which does restful registration and authentication and how to make sure that each and every request is accessed by validate user after authorization.

I understand, that i am asking lot . But please direct me to right place or righ book or blog which will give clear picture