ぺーぺーSEのブログ

備忘録・メモ用サイト。

Spring 3でHello World REST (Jersey)+HTTPメソッドの独自定義

Spring3+Jerseyにて独自のHTTPメソッドを追加してみる。


下記のコマンドを実行。(Maven 3を使用)

mvn archetype:generate -DgroupId=study
                       -DartifactId=Spring3HelloWorldJersey
                       -DarchetypeArtifactId=maven-archetype-webapp
                       -Dversion=1.0.0



pom.xmlを下記のように作成。

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>study</groupId>
  <artifactId>Spring3HelloWorldJersey</artifactId>
  <packaging>war</packaging>
  <version>1.0.0</version>
  <name>Spring3HelloWorldJersey Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <properties>
    <org.springframework.version>3.2.5.RELEASE</org.springframework.version>
    <jersey.version>1.17.1</jersey.version>
  </properties>

  <build>
    <finalName>Spring3HelloWorldJersey</finalName>
    <plugins>
      <!-- コンパイラの設定 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
      <groupId>com.sun.jersey.contribs</groupId>
      <artifactId>jersey-spring</artifactId>
      <version>${jersey.version}</version>
      <exclusions>
        <exclusion>
          <groupId>org.springframework</groupId>
          <artifactId>spring</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.springframework</groupId>
          <artifactId>spring-beans</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.springframework</groupId>
          <artifactId>spring-aop</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>com.sun.jersey</groupId>
      <artifactId>jersey-server</artifactId>
      <version>${jersey.version}</version>
    </dependency>
    <dependency>
      <groupId>com.sun.jersey</groupId>
      <artifactId>jersey-core</artifactId>
      <version>${jersey.version}</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.10</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>



次に以下を作成する。

  • 独自定義HTTPメソッドアノテーション(jp.sample.annotation.method.PROPFIND)
  • リソースインターフェース(jp.sample.resource.HelloResource)
    • HTTPリクエストに対する処理を記述クラス。
    • @Pathや@Getアノテーションを使用する。
  • リソースクラス(jp.sample.resource.impl.HelloResourceImpl)
    • リソースインターフェースの実装クラス
    • @Componentアノテーションをつけるのを忘れずに。(これをつけるとSpringが見つけてくれる)
  • applicationContext.xml
  • web.xml



■独自定義HTTPメソッドアノテーション(jp.sample.annotation.method.PROPFIND)

package jp.sample.annotation.method;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import javax.ws.rs.HttpMethod;

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@HttpMethod("PROPFIND")
public @interface PROPFIND {
}



■リソースインターフェース(jp.sample.resource.HelloResource)

package jp.sample.resource;

import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import jp.sample.annotation.method.PROPFIND;

@Path("/hello")
public interface HelloResource {

    @PROPFIND
    @Produces(MediaType.TEXT_PLAIN)
    public String sayHello();

}



■リソースクラス(jp.sample.resource.impl.HelloResourceImpl)

package jp.sample.resource.impl;

import jp.sample.resource.HelloResource;

import org.springframework.stereotype.Component;

@Component
public class HelloResourceImpl implements HelloResource {

    public String sayHello() {
        return "Hello World";
    }

}



■applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.0.xsd">

  <context:annotation-config />
  <context:component-scan base-package="jp.sample" />

</beans>



■web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>Jersey Spring Web Application</servlet-name>
    <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey Spring Web Application</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>

</web-app>



以上でサンプル実装完了。
サンプルを起動した後、BitKinex等のWebDAVクライアントツール
「http://[FQDN]:8080/Spring3HelloWorldJersey/hello」へアクセスするとステータスコード200が返ってくる。
何も表示されないけど。


参考:
http://my.safaribooksonline.com/book/programming/java/9780596809300/http-method-and-uri-matching/binding_http_methods