Spring Mobile To Detect Device In Java Web Application – Source Code on GitHub

Spring Mobile is an extension to Spring MVC framework. It primarily aims to simplify the development of mobile web applications.

Spring Mobile To Detect Device:

In this article, we will briefly look into the key usage of Spring Mobile and understand how to use Spring Mobile To Detect Device In Java Web Application.

Softwares used

  • Spring Boot 1.5.10.RELEASE
  • Spring Mobile
  • Thymeleaf
  • Java 8
  • Maven

Spring Mobile Features

The Spring Mobile provides capabilities to detect the type of device making a request to your web application. Below are a few key features that we can list:

1. Automatic Device Detection:

Spring Mobile provides a server-side device resolver abstraction layer. It analyzes all incoming requests and detects sender device information. The details are stored as part of the Device interface.

2. Site Preference Management:

With the inbuilt Site Preference Management, Spring Mobile allows users to choose between a “normal”, “mobile”, or “tablet” view of the website. This feature is less likely to be used as it depends on input from a user. There are other ways to achieve this using DeviceDelegatingViewresolver.

3. Site Switcher:

Site Switcher is capable of automatically switching the users to the most appropriate view according to his/her device type (i.e. mobile, desktop, etc.)

4. Device Aware View Manager:

Spring Mobile’s View Manager lets developer the flexibility to put all of the views in the pre-defined format and it organizes and manages different views for specific devices

Maven Dependencies

Detect Manually using Device Interface

There are two ways to serve device-specific contents. You can get hold of Device information and return the appropriate view depending on device type. Below is our such controller:

In this controller, we are setting one model attribute and using that in our thymeleaf view

Detect using Device Delegating View Resolver

In this approach, you simply define property in your application.properties file

Once you define this, the Spring will automatically configure a Device Delegating View Resolver. The resolver will detect the device and it will serve the view from the respective folder under templates.

E.g. If you are viewing the application from your mobile then an appropriate view from templates/mobile will be served. You can see the project structure:

Testing the Application

After we run our main application, it starts at URL http://localhost:8080

To simulate the different devices we will use Chrome: Simulate Mobile Devices with Device Mode. Using this you can select the device from the drop-down and refresh the URL.

Manual Detection and ServingDetect Device Using Spring Mobile

Testing with DeviceDelegatingViewresolver Enabled

If you enable this property then you need to have appropriate views defined for each of your URL or mapping.Spring Mobile To Detect Device


In this article, we’ve built a production-grade application that works perfectly on all platforms. Spring Mobile has reduced the development time of front-end centric application.

The complete source code is available to download from our GitHub repo.

Download Code

Add a Comment

Your email address will not be published. Required fields are marked *