基于Spring boot的设备类型检查

移动互联网时代用户的行为习惯逐渐往移动端迁移,越来越多的人通过手机浏览器来浏览网页。有时,我们需要知道一个用户是通过什么设备来访问我们的页面的,比如对比PC端访问量和移动端访问量。这些信息我们可以通过解析HTTP请求中的头信息来获取,但是这样和核心业务逻辑无关的代码放到Controller中实在不够优雅。没关系,Spring已经把这种脏活累活给干了!来看看基于Spring如何来做设备类型检查吧。

 

开发环境

  • IDE+Java环境(JDK 1.7或以上版本)
  • Maven 3.0+(Eclipse和Idea IntelliJ内置,如果使用IDE并且不使用命令行工具可以不安装)

pom.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.tianmaying</groupId>
  <artifactId>spring-boot-device-detection</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>spring-boot-device-detection</name>
  <description>use springboot to detect device type</description>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.5.RELEASE</version>
    <relativePath/>
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency> 
    <dependency>
      <groupId>org.springframework.mobile</groupId>
      <artifactId>spring-mobile-device</artifactId>
    </dependency> 
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

</project>

在POM中引入spring-mobile-device这个依赖之后,Spring Boot会自动配置好DeviceResolverHandlerInterceptorDeviceHandlerMethodArgumentResolver,其中:

DeviceResolverHandlerInterceptor通过Web请求头中的User-Agent属性来确定请求来自于桌面浏览器、移动浏览器还是平板浏览器。DeviceHandlerMethodArgumentResolver 则用于将DeviceResolverHandlerInterceptor拦截器捕获的信息转换为Device类,这样我们就能在Controller中,直接通过访问该类的属性就能检测出设备类型。

检测设备类型

我们通过Controller中的一个方法来返回设备信息,为了演示方便,这里我们不用HTML页面,让方法简单的返回一个字符串即可。@ResponseBody标注告诉Spring MVC直接将字符串作为Web响应(Reponse Body)返回,而不需要通过渲染HTML视图。

 

package com.tianmaying.springboot.devicedetection;

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
public class DeviceDetectionController {

    @RequestMapping("/detect-device")
    public @ResponseBody String detectDevice(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!";
    }

}

只要在方法签名中包含设备类型Device参数,Spring会自动把信息填充好,你拿来即用便好,恩,David喜欢"拿来即用"这个词,希望各种经验课程代码都能拿来即用,对我们提高开发效率是多棒的事情啊!

Run起来

通过Springboot把程序跑起来,不解释

 

package com.tianmaying.springboot.devicedetection;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App{

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

}

https://www.tianmaying.com/tutorial/spring-device-detection

猜你喜欢

转载自nethub2.iteye.com/blog/2332386