暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

基于Spring的设备类型检查

天码营 2016-06-08
136

点击上面“天码营”,加入我们,快速成长~

「内容简介」移动互联网时代用户的行为习惯逐渐往移动端迁移,越来越多的人通过手机浏览器来浏览网页,来看看基于Spring如何来做设备类型检查吧。

移动互联网时代用户的行为习惯逐渐往移动端迁移,越来越多的人通过手机浏览器来浏览网页。有时,我们需要知道一个用户是通过什么设备来访问我们的页面的,比如对比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会自动配置好DeviceResolverHandlerInterceptor
DeviceHandlerMethodArgumentResolver
,其中:

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);
    }
}

三个月时间如何成为Java Web全栈工程师?

点击下方“阅读原文”,可以获得更多天码营教程。

文章转载自天码营,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论