How to Detect Device Type in Spring MVC


Detecting device type in spring mvc application using spring library. Full details of spring-mvc configuration using classes DeviceWebArgumentResolver, DeviceResolverHandlerInterceptor, DeviceResolverRequestFilter.

Now days web application’s are designed to accessed by variety of mobile devices and most of the time it is required to detect device type on server. All web browsers and HTTP client sends user-agent with HTTP request by parsing those user-agent we can know device type like device is mobile, tablet or normal browser.detecting-device

For this Spring provides a library by using that you can easily get device type. Library name is spring-mobile-device.x.x.x.x.jar. To use this library in Spring MVC web application use following.

How to get lib

Maven Entry
<dependency>
    <groupId>org.springframework.mobile</groupId>
    <artifactId>spring-mobile-device</artifactId>
    <version>1.1.2.RELEASE</version>
</dependency>

You can also download it if not using maven.

Spring configuration

Add following Spring-MVC configuration
<mvc:annotation-driven>
    <mvc:argument-resolvers>
        <bean class="org.springframework.mobile.device.DeviceWebArgumentResolver" />
    </mvc:argument-resolvers>
</mvc:annotation-driven>
<mvc:interceptors>
       <bean class="org.springframework.mobile.device.DeviceResolverHandlerInterceptor" />
</mvc:interceptors>

Spring MVC DeviceWebArgumentResolver that resolves @Controller Method Parameters of type Device to the value of the web request\’s current device attribute.

DeviceResolverHandlerInterceptor is a Spring MVC interceptor that resolves the Device that originated the web request before any request handler is invoked. The resolved Device is exported as a request attribute under the well-known name ofDeviceUtils.CURRENT_DEVICE_ATTRIBUTE. Request handlers such as @Controllers and views may then access the currentDevice to vary their control and rendering logic, respectively.

Add following filter to web.xml
<filter>
    <filter-name>deviceResolverRequestFilter</filter-name>
    <filter-class>org.springframework.mobile.device.DeviceResolverRequestFilter</filter-class>
</filter>

DeviceResolverRequestFilter is Servlet 2.3 Filter that resolves the Device that originated the web request. The resolved Device is exported as a request attribute under the well-known name of DeviceUtils.CURRENT_DEVICE_ATTRIBUTE. Request handlers such as @Controllers and views may then access the currentDevice to vary their control and rendering logic, respectively.

How to use in controller

To use device detection in spring-mvc see below example

@RequestMapping("/home")
public @ResponseBody String detectDevice(String id, Device device) {
    String deviceType = "unknown";
    if (device.isNormal()) {
        deviceType = "normal";
    } else if (device.isMobile()) {
        deviceType = "mobile";
    } else if (device.isTablet()) {
        deviceType = "tablet";
    }
    return "Hello " + deviceType + " browser! Id:"+id;
}

Put org.springframework.mobile.device.Device type argument in you controller method where you want to detect device type.