2014年09月09日

癒しの一曲

なんか疲れてるんで...

生涯忘れられない場所がいくつかある
中には変わってしまった場所もあれば
永久に、悪くなってしまった場所
なくなった場所、昔のままの場所もあるけど
どの場所にもそれぞれに思い出がある
忘れられない恋人たちや友達との
死んでしまった人たち、元気でいる人たち
この人生で僕は、そのみんなを愛してきた


posted by NERDY at 23:54 | Comment(0) | 日記 | このブログの読者になる | 更新情報をチェックする

Jersey を使ってみる

Jerseyはオープンソースの、RESTful Web Services framework なんだそーです、ハイ。


1.プロジェクトのガラを作成
E:\develop>mvn archetype:generate ^
More? -DgroupId=com.example.rest ^
More? -DartifactId=rest-sample ^
More? -DarchetypeArtifactId=maven-archetype-webapp
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.2:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.2:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[INFO] Using property: groupId = com.example.rest
[INFO] Using property: artifactId = rest-sample
Define value for property 'version': 1.0-SNAPSHOT: :
[INFO] Using property: package = com.example.rest
Confirm properties configuration:
groupId: com.example.rest
artifactId: rest-sample
version: 1.0-SNAPSHOT
package: com.example.rest
Y: : Y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-webapp:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: E:\develop
[INFO] Parameter: package, Value: com.example.rest
[INFO] Parameter: groupId, Value: com.example.rest
[INFO] Parameter: artifactId, Value: rest-sample
[INFO] Parameter: packageName, Value: com.example.rest
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: E:\develop\rest-sample
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 21.049 s
[INFO] Finished at: 2014-09-09T14:27:26+09:00
[INFO] Final Memory: 10M/25M
[INFO] ------------------------------------------------------------------------
E:\develop>


2.src/main/java/com/example/rest フォルダを作成する

3.作成したフォルダに RestSample.java を配置する
package com.example.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path("restsample")
public class RestSample {
  @GET
  // メソッド名はなんでもいいみたい。
  public String sample() {
    return "今日はスーパームーン!";
  }
}


4.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>com.example.rest</groupId>
 <artifactId>rest-sample</artifactId>
 <packaging>war</packaging>
 <version>1.0</version>
 <name>rest-sample Maven Webapp</name>
 <url>http://maven.apache.org</url>
 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>
 <dependencies>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>3.8.1</version>
   <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>2.6</version>
  </dependency>
 </dependencies>
 <build>
  <finalName>rest-sample</finalName>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
     <source>1.8</source>
     <target>1.8</target>
    </configuration>
   </plugin>
   <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-war-plugin</artifactId>
     <version>2.4</version>
     <configuration>
       <!-- web.xml を削除してるので、ここの指定がないとエラーになる -->
       <failOnMissingWebXml>false</failOnMissingWebXml>
     </configuration>
   </plugin>
  </plugins>
 </build>
</project>


5.Application クラスの作成
  JerseyResourceConfig を継承して作成する。
package com.example.rest;

import javax.ws.rs.ApplicationPath;
import org.glassfish.jersey.server.ResourceConfig;

@ApplicationPath("rest")
public class RestConfig extends ResourceConfig {
  public RestConfig() {
    packages(this.getClass().getPackage().getName());
  }
}


6.web.xml を削除する
  Application クラスを作成したので、web.xml は不要。


7.war を作成する
E:\develop\rest-sample>mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building rest-sample Maven Webapp 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ rest-sample ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ rest-sample ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to E:\develop\rest-sample\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ rest-sample ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\develop\rest-sample\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ rest-sample ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ rest-sample ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-war-plugin:2.4:war (default-war) @ rest-sample ---
[INFO] Packaging webapp
[INFO] Assembling webapp [rest-sample] in [E:\develop\rest-sample\target\rest-sample]
[INFO] Processing war project
[INFO] Copying webapp resources [E:\develop\rest-sample\src\main\webapp]
[INFO] Webapp assembled in [6259 msecs]
[INFO] Building war: E:\develop\rest-sample\target\rest-sample.war
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 16.114 s
[INFO] Finished at: 2014-09-09T15:22:37+09:00
[INFO] Final Memory: 14M/35M
[INFO] ------------------------------------------------------------------------
E:\develop\rest-sample>


8.ブラウザで表示
  target フォルダに作成された、rest-sample.war を、 tomcat に配置。
  http://localhost:8080/rest-sample/rest/restsample にアクセス。
  restsample.jpg


無事、動いてくれましたとさ。
posted by NERDY at 15:30 | Comment(0) | 技術 | このブログの読者になる | 更新情報をチェックする
×

この広告は180日以上新しい記事の投稿がないブログに表示されております。