Detecting mobiles and tablets

In this recipe, you'll learn how, from a controller method, you can detect whether the current HTTP request has come from a desktop computer, mobile, or tablet.

How to do it…

Register a DeviceResolverHandlerInterceptor interceptor and use DeviceUtils in the controller method:

  1. In the Spring configuration class, declare a DeviceResolverHandlerInterceptor bean:
    @Bean
    public DeviceResolverHandlerInterceptor deviceResolverHandlerInterceptor() {
        return new DeviceResolverHandlerInterceptor();
    }
  2. Register the DeviceResolverHandlerInterceptor bean as an interceptor in the addInterceptors() method:
    @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(deviceResolverHandlerInterceptor()) ; } ...

Get Spring Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.