Spring 4 Mobile: Detect Device Example Using Annotation

By Arvind Rai, August 24, 2014
On this page, we will describe Spring 4 Mobile Detect Device Example using Annotation. Detecting device means, we can identify from which device, web request is coming. In Controller class method, Device class can be used as an argument that will identify the web request device. To use Device class, we will configure DeviceWebArgumentResolver and DeviceResolverRequestFilter in our application. Find the complete example step by step.

Software Requirement

To run the demo, I am using below software.
1. JDK 6
2. Tomcat 7
3. Eclipse
4. Grdale

Configuration Class : Use DeviceWebArgumentResolver

org.springframework.mobile.device.DeviceWebArgumentResolver plays main role to detect device. Use of DeviceWebArgumentResolver allows controller method to use org.springframework.mobile.device.Device class as an argument.
AppConfig.java
package com.concretepage.config;  
import java.util.List;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.mobile.device.DeviceWebArgumentResolver;
import org.springframework.mobile.device.site.SitePreferenceHandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.ServletWebArgumentResolverAdapter;
@Configuration 
@ComponentScan("com.concretepage") 
@EnableWebMvc   
public class AppConfig extends WebMvcConfigurerAdapter  {  
	   @Override
	   public void addArgumentResolvers(
	       List<HandlerMethodArgumentResolver> argumentResolvers) {
	     argumentResolvers.add(new ServletWebArgumentResolverAdapter(
	         new DeviceWebArgumentResolver()));
	     argumentResolvers
	         .add(new SitePreferenceHandlerMethodArgumentResolver());
	   }
} 

WebApplicationInitializer Class : Add DeviceResolverRequestFilter

org.springframework.mobile.device.DeviceResolverRequestFilter needs to be added in WebApplicationInitializer class. DeviceResolverRequestFilter resolve the device coming from web request.
WebAppInitializer.java
package com.concretepage.config;
import java.util.EnumSet;
import javax.servlet.DispatcherType;
import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;
import org.springframework.mobile.device.DeviceResolverRequestFilter;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class WebAppInitializer implements WebApplicationInitializer {
	public void onStartup(ServletContext servletContext) throws ServletException {  
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();  
        ctx.register(AppConfig.class);  
        ctx.setServletContext(servletContext);    
        Dynamic dynamic = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));  
        dynamic.addMapping("/");  
        dynamic.setLoadOnStartup(1);
        FilterRegistration.Dynamic filterDynamic = servletContext.
        		addFilter("deviceResolverRequestFilter", new DeviceResolverRequestFilter());
        EnumSet<DispatcherType> dispatcherTypes = EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD);
        filterDynamic.addMappingForUrlPatterns(dispatcherTypes, true, "/*");
   }  
} 

Controller Class : Use Device

org.springframework.mobile.device.Device has three Boolean method that identify the device. These are Device.isMobile(), Device.isTablet() and Device.isNormal() methods.
DeviceController.java
package com.concretepage;
import org.springframework.mobile.device.Device;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/detect")
public class DeviceController {
	@RequestMapping("/device")
	public @ResponseBody String getDeviceInfo(Device device) {
		String msg ="";
		if(device.isMobile()){
			msg = "Your are Mobile User.";
		}
		else if(device.isTablet()){
			msg = "Your are Tablet User.";
		}
		else if(device.isNormal()){
			msg = "Your are Normal Browser User.";
		}
		return msg;
	}
}	 

JAR Dependency Using Gradle

Find the Jar dependency Spring web and Mobile as a gradle script.
build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'war'
archivesBaseName = 'Spring4'
version = '1' 
repositories {
    mavenCentral()
}

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-web:1.1.5.RELEASE'
    compile  'org.springframework.mobile:spring-mobile-device:1.1.2.RELEASE'
    compile  'com.fasterxml.jackson.core:jackson-databind:2.4.2' 
} 

Output of Detect Device Example

To run the demo, create WAR using the build.gradle and deploy WAR into tomcat. Access the URL as http://localhost:8080/Spring4-1/detect/device and check the output. Test in different device like Mobile and Tablet. In our case, we are accessing the URL in normal computer browser.
Spring 4 Mobile:  Detect Device Example Using Annotation

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI







©2024 concretepage.com | Privacy Policy | Contact Us