Spring into Mobile Application Development |
|

At SpringOne2gx we announced exciting new initiatives in the areas of social media and mobile application development. A few weeks ago, Craig Walls released Spring Social. Today, Roy Clarkson released Spring Mobile and Spring Android. In this post, I'd like to highlight these projects and share how Spring aims to simplify mobile application development.
Choices in Mobile Application Development
If you attended SpringOne2gx this year, you've seen Greenhouse, an app we built for our community that also serves as a reference and driver for Spring technology. Craig showed you some of the social elements of Greenhouse, such as the ability to connect your account with Twitter and Facebook. There are also a number of mobile elements. Specifically, Greenhouse doubles as a mobile web app, and sports native Greenhouse for iPhone and Android clients.
Like many organizations today, we had to answer the basic question of "what mobile platforms to target?" In the end, we chose to invest in a native experience for iPhone and Android users, while also developing a cross-platform mobile web app. Our decision to go native was driven by the fact the app is consumer-oriented, and a large number of prospective consumers (application developers) own iPhone and Android devices. At the same time, the mobile web app aims to provide a good baseline experience that works cross-platform, something that's possible today with the rise of WebKit and HTML 5.
From our development work, grew Spring Framework contributions: first, a Spring Mobile project that provides extensions to Spring MVC for developing mobile web apps; and second, a Spring Android project that supports the development of native Android clients that communicate with Spring-based back-ends. I'll take you through each of these projects in turn.
Spring Mobile
The first problem we tackled was designing a web app that was pleasant for mobile visitors to use. While a smartphone may have a capable web browser, it still has a small screen and that needs to be accounted for. There are essentially two approaches to dealing with this problem:
- Detect the device that originated the web request and serve a separate site to mobile devices.
- Serve a single site, but progressively enhance it for desktop users by using CSS 3 media queries and JavaScript.
You can find examples of both techniques in the wild today; for example, the conference site Lanyrd enhances through client-side detection, while SpeakerRate uses server-side detection that redirects mobile visitors to a separate site (see for yourself by installing the Firefox User Agent Switcher and setting your user agent to iPhone).
In Greenhouse, we started with server-side detection. Specifically, we aimed to apply a different page layout if the device was a mobile device. Out of this grew a general purpose "Device Resolver Abstraction", which is the defining feature of Spring Mobile 1.0.0.M1. Some highlights of this feature include:
- A HandlerInterceptor that uses a DeviceResolver to detect the Device that originated the current HttpServletRequest.
- The ability to inject the detected Device into @Controller methods and view templates to vary logic by device type.
Below are some usage examples from the Greenhouse codebase. First, have a look at the interceptor definition from /WEB-INF/spring/appServlet/servlet-context.xml:
<interceptors>
<!-- On pre-handle, detect the device that originated the web request -->
<beans:bean class="org.springframework.mobile.device.mvc.DeviceResolvingHandlerInterceptor" />
</interceptors>
Now, a JSP template that conditionally renders some content if the device is not a mobile device:
Please try again<c:if test="${!currentDevice.mobile}"> or <a href="<c:url value="/signup" />">sign up</a></c:if>.
Finally, a Tiles-based page layout that changes if the device is a mobile device:
<definition name="page" templateExpression="/WEB-INF/layouts/${currentDevice.mobile ? 'mobile/' : 'standard/'}page.jsp" />
In addition to these features, Spring Mobile 1.0.0.M1 ships:
- A HandlerInterceptor that redirects mobile visitors to another URL. This is useful if your mobile site truly is a separate application. Consider Flickr, for example, which will redirect you to m.flickr.com if you access www.flickr.com from your phone.
- A DeviceResolver implementation that delegates to WURFL for device detection. WURFL provides a large database of devices and their capabilities. It is useful when you need to know more about the Device that originated the request, such as its specific screen size, manufacturer, model, preferred markup, or other capabilities.
Recently, we have also begun exploring the use of CSS 3 media queries with JavaScript to perform client-side detection. This approach has the advantage of not requiring special server-side handling unless you aim to vary the semantic content you send to the device, and not just optimize the style. Not all browsers support media queries, so they may not be an option for you, but if you're targeting smartphones with WebKit-based browsers you should be fine. In general, the approach of first designing your app for mobile, then progressively enhancing it for desktop feels quite elegant to me.
Spring Android
For the Android client, a different set of challenges emerged. We needed to exchange data with the server over HTTPS via REST, and since that data is user-specific we require the user to sign in. Rather than use Basic Auth, where we would need to store username and password credentials on the device itself, we opted for OAuth.
OAuth is an emerging standard that provides a token-based authorization scheme. Essentially, a username and password is traded for an access token, and the access token is used to make requests for protected resources. This means you only need to store the access token with the device for "remember me" functionality. Furthermore, we chose to implement the sign in process whereby the client takes you to the server's web site for connection authorization. In this way, the client never sees your username and passsword, which is important if you allow third-party clients to be developed against your API (which we also want to encourage). Finally, in the event a user's phone is stolen or otherwise compromised, an access token can be invalidated without the risk of username and password compromise.
Out of this work, came the desire to get specific modules of the Spring Framework working in an Android environment. Specifically, we aimed to use RestTemplate for the REST API calls, and Spring Security for the OAuth client.
I'm pleased announce that the first milestone of Spring Android ships an "Android-ready" RestTemplate. We're using it in Greenhouse, and we encourage you to use it as the REST client in your own Android applications. Check out the example usage below:
RestTemplate restTemplate = new RestTemplate(new CommonsClientHttpRequestFactory());
Event event = restTemplate.getForObject("https://myapp.com/event/{name}", Event.class, "springone2gx");
In future milestones, you can expect more of the Spring Framework to be supported in an Android environment, such as the Spring Security OAuth client.
Getting Started
The best way to get started is to see it live by walking through the Greenhouse app in your own local development environment. As a reference, Greenhouse uses the Spring Mobile and Spring Android projects, as well as Spring MVC, Security, Social, and Integration. The project page provides a guide that shows you how to get the web app, iPhone client, and Android client all running in your local environment in minutes.
If you have questions about features, roadmap, or just want to have a discussion with the development team, please do visit our community forums. We value your feedback.
Summary
I'm very excited about all the new initiatives we've got going, and especially what we're doing in the social and mobile space. The first milestones of these projects are just the beginning. I encourage you to get involved in the projects that are useful to you and help us make them the best they can possibly be.
Similar Posts
- Spring Social 1.0: What a Year Makes
- Socializing Spring Applications
- Spring Android and Maven (Part 1)
- Spring Android and Maven (Part 2)
- Extending Spring Social's Service Provider Framework





Jochen Szostek says:
Added on November 19th, 2010 at 7:37 pmWow, I've been thinking about creating my own springy kinda framework as soon as I had the time, but you guys already have something available now. This imo is awesome news.
Best wishes with the project, I'm sure it will become another standard way of resolving issues in a java environment.
Rohit says:
Added on November 20th, 2010 at 10:22 amThis is great news ! Looking forward to all the future releases.
By the way https://greenhouse.springsource.org/ is down at the moment
cheers !
rohit
Keith Donald (blog author) says:
Added on November 20th, 2010 at 11:04 amRohit,
The Greenhouse public instance appears to be running fine from our end. It's monitored by Hyperic, so we will receive an alert if the app is not available. I'm just about to load in the data for the upcoming Rich Web Experience conference, so you'll see that in there soon as well.
Thanks for the feedback!
Keith
Rohit says:
Added on November 21st, 2010 at 12:05 amKeith:
Thanks for your reply. I am still not able to to do anything with greenhouse. This is what happened yesterday and this is what is happening now –
1) Hit the URL https://greenhouse.springsource.org
2) It comes up
3) click on Register Me link (https://greenhouse.springsource.org/signup)
4) It breaks – comes up with
"HTTP Status 500 -type Exception report message
description The server encountered an internal error () that prevented it from fulfilling this request.exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is ava.lang.NumberFormatException: For input string: "5.5"
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:656) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)"
5) Now try accessing the main URL again (step 1 above which worked earlier), it also breaks now – with the same exception trace as shown in point 4 above.
PS: I have taken the screenshots – let me know if you want me to email them to you.
cheers !
rohit
Keith Donald (blog author) says:
Added on November 21st, 2010 at 11:12 amRohit,
Unfortunately, I am unable to reproduce your problem on either Firefox, Safari, or Chrome browsers. We also have had a number of sign ups today and yesterday.
Would you mind creating a JIRA that describes your environment, including your browser version, and contain the exact reproduction steps, full exception stack trace, and screenshots. The JIRA is definitely the right place to report and discuss specific issues with the app and we'll take a more detailed look from there.
Thanks!
Keith
Zdenek Farana says:
Added on November 21st, 2010 at 9:16 pmWow, just in time when I'm becoming Android developer… Thanks!
Rohit says:
Added on November 21st, 2010 at 10:59 pmKeith:
Done ! GREENHOUSE-486 is the defect number and here is the link https://jira.springframework.org/browse/GREENHOUSE-486
For what it's worth I have also uploaded the screenshots.
PS: waiting to get registered
cheers !
rohit
James Heggs says:
Added on November 22nd, 2010 at 4:14 amHi Keith,
I have currently been working on a library called AsyncAndroid which allows user to communicate with a Remote server asynchronously using Android.
It makes use of HttpClient and the Gson library to convert JSON response to POJO's.
I'm currently finalising the library and trying to decouple the response from the updating of the UI so it is still somewhat of a work in progress but it may be worth the Spring guys taking a look on the implementation.
The project has been influenced by my usage of Spring and its automatic Json building views as well as my use of Google Web Toolkit and Asynchronous communication.
The project is hosted on Google Code at:
http://code.google.com/p/asyncandroid/
Hope it might help somewhat,
James
paul says:
Added on November 23rd, 2010 at 12:21 amgreat post. i wanna share also a spoon-feed tutorial on Spring MVC
http://www.adobocode.com/spring/a-spring-web-mvc-tutorial
hope it will help people!
Towhidul Haque Roni says:
Added on November 23rd, 2010 at 1:09 amawesome. i’m looking for this kidda staffs. i am a open source and spring lover. all the best for this mobile project.
galen says:
Added on November 23rd, 2010 at 4:53 amhi, Keith,
when i run android client, found following error
[2010-11-23 17:19:06 - Greenhouse for Android] Failed to install Greenhouse for Android.apk on device 'emulator-5554!
[2010-11-23 17:19:06 - Greenhouse for Android] (null)
[2010-11-23 17:19:07 - Greenhouse for Android] Launch canceled!
[2010-11-23 17:28:36 - Greenhouse for Android] warning: Ignoring InnerClasses attribute for an anonymous inner class that doesn't come with an associated EnclosingMethod attribute. (This class was probably produced by a broken compiler.)
[2010-11-23 17:28:37 - Greenhouse for Android] warning: Ignoring InnerClasses attribute for an anonymous inner class that doesn't come with an associated EnclosingMethod attribute. (This class was probably produced by a broken compiler.)
[2010-11-23 17:31:24 - Greenhouse for Android] ——————————
Dan Parker says:
Added on November 23rd, 2010 at 12:27 pmIs it possible to define a different controller to handle the request depending on if it's a mobile device or not? In terms of separation of concerns, I'd have though that this approach would be more suitable?
i.e. what you display to the user is likely to be different depending on the device, there may be little or no cross over in terms of data required in the model. You *could* continue to populate the model with all data (mobile and non) regardless, but this seems wasteful. Alternatively you could introduce logic into the controller to choose what data is populated depending on the device, but then this logic will be scattered throughout every controller. Either way you still need some logic to decide which view to display in each controller or view.
It would be nice if there was an attribute of the @RequestHandler where you could specify a device and then the dispatcher dispatches accordingly.
This would also improve the testability and readability of the resulting code as you'd end up with two controllers each dealing with different situations, rather than one controller that tries to handle it all
Arindam Biswas says:
Added on February 1st, 2011 at 4:11 amHi Keith,
Greenhouse webapp error :
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is ava.lang.NumberFormatException: For input string: "5.5"
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:656)
appears to all those from India cause we are 5 hours and 30 mins (5.5) ahead of GMT. Code takes in a number and not float.
Thanks,
Arindam
SK says:
Added on February 4th, 2011 at 5:03 amHi Keith,
I need one concern from you.
Our web application is running on weblogic server.
Our plan is to upgrade the WEB applications to WAP enabled application so the user can also view the same in mobile devices.
So can you please shed light how come we can come up with solution using Spring for this requirement?
Our intention is to create an asset/tool so that it can promote all our WEB applications (running on Weblogic and JBoss) to WAP.
Appreciate for your valued reply.
regards
SK
Sandeep Singh says:
Added on March 15th, 2011 at 5:34 amThis is great news ! Looking forward to all the future releases.
Best wishes with the project.
australia mobile says:
Added on April 3rd, 2011 at 2:56 pmgreat news , I have join a organizational as a Mobile Application Development and this post help to me . Thanks
Nick says:
Added on July 15th, 2011 at 6:45 amThanks…..for the post
The Taj Mahal App : One of the World's TOP human-built WONDERS arrives on iPad : DOWNLOAD TODAY
Mobile Application Development Company says:
Added on August 2nd, 2011 at 6:31 amVery informational write up… Thanks for valuable info
Treasure Hunt App says:
Added on August 11th, 2011 at 3:47 amGreat information thanks for sharing this with us.In fact in all posts of this blog their is something to learn . your work is very good and i appreciate your work and hopping for some more informative posts . Again thanks
Jacob Dixon says:
Added on October 11th, 2011 at 5:56 amIts a very informative stuffs available here in this blog and lots of clearification had been made on iPhone as well as Android users point of view, which could be beneficial to them, great work keep it on
Vladimir says:
Added on October 12th, 2011 at 8:22 amCouple notes on M3 release:
The class org.springframework.mobile.device.LiteDeviceResolver is out of date. It is not resolving some modern mobile devices. Do you have any plans for an update?
Developers can extend the LiteDeviceResolver fixing the device resolution logic. However, I think, it would be the best making it configurable…
Regards.
Text Marketing says:
Added on October 19th, 2011 at 4:20 amThe mobile industry is so vast. Everything is so possible on computers and mobile phones right now.
Tharindu says:
Added on October 31st, 2011 at 10:51 pmVery good post helps a lot. Good luck keep it up.
http://www.blazemedea.com
web development services says:
Added on November 29th, 2011 at 2:08 amHi this is jack Dowson I just read your blog about Web Design, such a great list of blogs about web design and I agree with your list. These are really resourceful and top related blogs on web designs. I was searching about web designing and I found your website with useful information, thanks for information.
Sam Daniel says:
Added on December 29th, 2011 at 7:20 amThis very useful post, thanks for sharing it. The Mobile Application industry offers endless opportunities.
rakesh says:
Added on January 8th, 2012 at 10:47 amThanks for giving information about this field. now with HTML 5 browsers are also changing very rapidly for fully support to HTML 5.
Online Free videos
mobiddiction says:
Added on January 13th, 2012 at 1:20 amExcellent details thanks for discussing this with us
Mobile Apps Development says:
Added on February 15th, 2012 at 2:00 amThanks For sharing your Knowledge. Mobile phone Development Companies always giving surprises with new applications.
Warner Piñero says:
Added on February 21st, 2012 at 4:00 amIs there a book for learning Spring-Android? Can somebody post the link where I can purchase one?
Thanks
High SERP says:
Added on March 21st, 2012 at 12:43 amThanks for this great article! Definitely want to get more into the mobile computing field as it continues to expand.
R R says:
Added on May 16th, 2012 at 4:36 amthank you for sharing the information…
Samanyu is one place for all solutions regarding mobile applications
Terry Sommon says:
Added on May 29th, 2012 at 2:50 pmYour mobile website visitors gain the following benefits…
- They get quick access to directions to your business.
- They can call you with one click on their mobile phone.
- They can easily add your business phone number to their
mobile phone address book.
- They can easily bookmark your mobile website address for
fast future reference.
Very few of your competitors are using the new mobile web technology. Get in now and start leading the pack. If your company does not have a mobile presence, you risk losing ground to your competition that do. You also risk failing to connect with users, and miss out on opportunities to extend and strengthen your brand.
http://gomobile.searchbarbadosonline.com/
BeetleRim IT Services says:
Added on July 4th, 2012 at 4:42 amImprovements include a smoother and more responsive UI across the system, a home screen that automatically adapts to fit your content, a powerful predictive keyboard, richer and more interactive notifications, larger payload sizes for Android Beam sharing and much more. For a lowdown on what’s new, head over to the Jelly Bean platform highlights.
sblsoftware00 says:
Added on July 5th, 2012 at 12:38 amNice blog!…….Our team has successfully mapped the business process requirements of all our clients consistently.
SBL focuses on delivering the Best, Cost-effective solutions, and Value added services on-time to meet a wide range of requirements, in order to help clients acheive greater returns on investments for their technology initiatives.
SBL combine our mobile application development expertise, innovative research techniques and new approaches together to provide you the best solution.If you would like to know more about the different services we offer please feel free to contact us
SBL
1B, 1st Floor,
Leela Infopark – Phase – II
Kakkanad, Cochin,
India – 682030
Tel: 91 (0) 859 023 3999
Fax: 91 (0) 484 2784056
Mobile Apps Development Team says:
Added on July 18th, 2012 at 2:14 amThanks for this great and informative post. Application market is always providing something new to the customers and therefore mobile application development industry is booming today. Great to see Spring into mobile apps development.
Mobile Application Developers says:
Added on August 20th, 2012 at 3:02 amThanks for sharing this wonderful knowledge with us.I'll look forward to it.
Phone App Developer says:
Added on October 1st, 2012 at 5:13 amThank you for the informations about Mobile Application Development! Looking forward to more of your articles.
mobile application development says:
Added on October 18th, 2012 at 3:42 amThanks for this great and informative post. Looking forward to more of your articles.
Aaric says:
Added on November 17th, 2012 at 1:00 pmI think more people need to read blogs like this. Its so important to know how to construct a great blog to get people interested and youve done just that. assignment writing assistance uk | who to get assignment | speech assistance uk