---
title: Spring MVCからWootheeを使う
tags: ["Java", "Spring", "Spring Boot", "Spring MVC", "Woothee"]
categories: ["Programming", "Java", "is", "tagomor", "woothee"]
date: 2015-01-03T16:40:31Z
updated: 2015-01-03T16:40:31Z
---

[@tagomoris](https://twitter.com/tagomoris)さん作のUser-Agent判定器[WootheeのJava版](https://github.com/woothee/woothee-java)をSpring MVCから使ってみます。

### 普通に使ってみる

Woothee自体は依存関係に以下を追加すれば使えます。

``` xml
<dependency>
    <groupId>is.tagomor.woothee</groupId>
    <artifactId>woothee-java</artifactId>
    <version>1.0.0</version>
</dependency>
```

基本的にUser-Agentを`is.tagomor.woothee.Classifier`に渡すだけなので、Spring MVCでは`@RequestHeader`でUser-Agentを取得すれば良いです。

``` java
@SpringBootApplication
@RestController
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

    @RequestMapping(value = "/")
    String hello(@RequestHeader("User-Agent") String userAgent) {
        Map<String, String> result = Classifier.parse(userAgent);
        boolean isCrawler = Classifier.isCrawler(userAgent);
        System.out.println(result);
        System.out.println("isCrawler=" + isCrawler);
        return "Hello World!";
    }
}
```
(例はSpring Bootですが、ただのSpring MVCアプリの例です)

以下のように出力されます。

```
{name=Chrome, category=pc, os=Mac OSX, version=39.0.2171.95, vendor=Google, os_version=10.9.5}
isCrawler=false
```

### HandlerMethodArgumentResolverを使う

毎回User-Agentを取って渡すというのも面倒くさいので、判定結果をオブジェクトに設定して、そのオブジェクトをControllerに引数に取りたいです。このような用途にはSpring MVCの[`HandlerMethodArgumentResolver`](http://terasolunaorg.github.io/guideline/1.0.1.RELEASE/ja/ImplementationAtEachLayer/ApplicationLayer.html#handlermethodargumentresolver)が使えます。

今回は、`WootheeHandlerMethodArgumentResolver`という簡単な`HandlerMethodArgumentResolver`実装を[用意した](https://github.com/making/woothee-spring)のですぐに使えます。

依存関係に以下を追加します。

``` xml
<dependency>
    <groupId>am.ik.woothee</groupId>
    <artifactId>woothee-spring</artifactId>
    <version>1.0.0</version>
</dependency>
```

Spring Boot(というかJava Config)で`WootheeHandlerMethodArgumentResolver`を追加するには以下のように定義します。この設定により、Controllerの引数に`am.ik.woothee.Woothee`オブジェクトを取れます。

``` java
import am.ik.woothee.Woothee;
import am.ik.woothee.spring.WootheeMethodArgumentResolver;
// 略

import java.util.List;

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

    @Configuration
    static class WootheeConfig extends WebMvcConfigurerAdapter {
        @Override
        public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
            argumentResolvers.add(new WootheeMethodArgumentResolver());
        }
    }

    @RequestMapping(value = "/")
    String hello(Woothee woothee) {
        System.out.println(woothee);
        return "Hello World!";
    }
}
```

以下のように出力されます。

```
WootheeData{category='pc', name='Chrome', version='39.0.2171.95', os='Mac OSX', vendor=Google, osVersion=10.9.5}
```

XMLで定義したい場合は、

``` xml
<mvc:annotation-driven>
    <mvc:argument-resolvers>
        <bean
            class="am.ik.woothee.spring.WootheeMethodArgumentResolver" />
    </mvc:argument-resolvers>
</mvc:annotation-driven>
```

Spring MVCアプリでUser-Agentを判定したい場合にどうぞ！
