2011년 5월 24일 화요일

JAX-RS: Java™ API for RESTful Web Services - 2

HelloWorldWebApp 개발 해보기
개발 환경 Eclipse 3.3(http://www.eclipse.org)
1. “HelloWorldWebApp” Java Project 생성
2. Java Build Path 아래 Library 추가
activation.jar
asm-3.1.jar
jaxb-api.jar
jaxb-impl.jar
jdom-1.0.jar
jersey.jar
jettison-1.0-RC1.jar
jsr173_api.jar
jsr250-api.jar
jsr311-api.jar
mail.jar
rome-0.9.jar


3. HelloWorldResoure.java
package com.naver.blog.inter999.jaxrs.sample;
import javax.ws.rs.GET;
import javax.ws.rs.ProduceMime;
import javax.ws.rs.Path;
@Path("/helloworld")
public class HelloWorldResource {
@GET
@ProduceMime("text/plain")
public String getClichedMessage() {
return "Hellow World [환영 합니다].!";
}
}


4. web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>com.sun.ws.rest.spi.container.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>


5. Ant build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." default="war.build" name="HelloWorldWebApp">
<property environment="env"/>
<property name="debuglevel" value="source,lines,vars"/>
<property name="target" value="1.5"/>
<property name="source" value="1.5"/>
<property name="build.path" value="./build" />
<property name="jersey.path" value="../../../library/jersey-0.8" />
<property name="library.path" value="${jersey.path}/lib" />
<path id="build.classpath">
<pathelement location="${build.path}/classes"/>
<pathelement location="${library.path}/activation.jar"/>
<pathelement location="${library.path}/asm-3.1.jar"/>
<pathelement location=".${library.path}/jaxb-api.jar"/>
<pathelement location="${library.path}/jaxb-impl.jar"/>
<pathelement location="${library.path}/jdom-1.0.jar"/>
<pathelement location="${library.path}/jettison-1.0-RC1.jar"/>
<pathelement location="${library.path}/jsr173_api.jar"/>
<pathelement location="${library.path}/jsr250-api.jar"/>
<pathelement location="${library.path}/jsr311-api.jar"/>
<pathelement location="${library.path}/mail.jar"/>
<pathelement location="${library.path}/rome-0.9.jar"/>
<pathelement location="${jersey.path}/dist/jersey.jar"/>
</path>
<target name="prepare">
<tstamp />
<mkdir dir="${build.path}" />
<mkdir dir="${build.path}/WEB-INF" />
<mkdir dir="${build.path}/WEB-INF/classes" />
<mkdir dir="${build.path}/WEB-INF/lib" />
</target>
<target name="clean">
<delete dir="${build.path}" />
</target>
<target depends="prepare" name="compile">
<javac debug="true" debuglevel="${debuglevel}" destdir="${build.path}/WEB-INF/classes" source="${source}" target="${target}">
<src path="./src"/>
<classpath refid="build.classpath"/>
</javac>
</target>
<target name="war.build" depends="compile">
<copy file="${library.path}/activation.jar" tofile="${build.path}/WEB-INF/lib/activation.jar" overwrite="true" />
<copy file="${library.path}/asm-3.1.jar" tofile="${build.path}/WEB-INF/lib/asm-3.1.jar" overwrite="true" />
<copy file="${library.path}/jaxb-api.jar" tofile="${build.path}/WEB-INF/lib/jaxb-api.jar" overwrite="true" />
<copy file="${library.path}/jaxb-impl.jar" tofile="${build.path}/WEB-INF/lib/jaxb-impl.jar" overwrite="true" />
<copy file="${library.path}/jdom-1.0.jar" tofile="${build.path}/WEB-INF/lib/jdom-1.0.jar" overwrite="true" />
<copy file="${library.path}/jettison-1.0-RC1.jar" tofile="${build.path}/WEB-INF/lib/jettison-1.0-RC1.jar" overwrite="true" />
<copy file="${library.path}/jsr173_api.jar" tofile="${build.path}/WEB-INF/lib/jsr173_api.jar" overwrite="true" />
<copy file="${library.path}/jsr250-api.jar" tofile="${build.path}/WEB-INF/lib/jsr250-api.jar" overwrite="true" />
<copy file="${library.path}/jsr311-api.jar" tofile="${build.path}/WEB-INF/lib/jsr311-api.jar" overwrite="true" />
<copy file="${library.path}/mail.jar" tofile="${build.path}/WEB-INF/lib/mail.jar" overwrite="true" />
<copy file="${library.path}/rome-0.9.jar" tofile="${build.path}/WEB-INF/lib/rome-0.9.jar" overwrite="true" />
<copy file="${jersey.path}/dist/jersey.jar" tofile="${build.path}/WEB-INF/lib/jersey.jar" overwrite="true" />
<copy file="./WEB-INF/web.xml" tofile="${build.path}/WEB-INF/web.xml" overwrite="true" />
<jar jarfile="${basedir}/${ant.project.name}.war">
<fileset dir="./build"/>
</jar>
</target>
</project>


6. HelloWorldWebApp Directory 구조
7. Tomcat webapp Deploy http://localhost:8080/HelloWorldWebApp/helloworld 에서 확인
TIP
Jersey 포함된 Sample Glassfish 이용하도록 되어있다. Tomcat에서 Sample 경우 Class 없다는 경보가 발생하는 경우가 있는데 jersey/lib jar 파일들을 Tomcat/lib 복사해 두면 경보를 피할 있다.


Sample 포함된 JAX-RS 기본 문법
1. @ProduceMime – Method HTTP응답의 반환값 MIME Type 지정한다.
@ProduceMime(“*/*”)
@ProduceMime(“type”)
@ProduceMime({“type”,”type”,”type”})


2. @ComsumeMime – 받아들이는 MIME Type 지정한다.
@ComsumeMime(“*/*”)
@ConsumeMime(“type”)
@ConsumeMime({“type”,”type”,”type”})


3. @Path – URI 구성에 사용하는 Annotation

댓글 없음:

댓글 쓰기

ETL 솔루션 환경

ETL 솔루션 환경 하둡은 대용량 데이터를 값싸고 빠르게 분석할 수 있는 길을 만들어줬다. 통계분석 엔진인 “R”역시 하둡 못지 않게 관심을 받고 있다. 빅데이터 역시 데이터라는 점을 볼때 분산처리와 분석 그 이전에 데이터 품질 등 데이...