2009年12月11日 星期五

使用-jersey-tomcat-輕鬆開發-restful-web-service

這是一篇簡易教學文章, 可以簡單帶出 Restful WebService使用概念.

附帶一提,必須run在Tomcat 6.0才行,5.5版本會有Exception出現:

Caused by: java.lang.ClassNotFoundException: javax.persistence.PersistenceUnit

至於原因,若有哪位高手願意指教,十分感謝。 ^^

底下簡單列一下目前所使用的版本:

  • Eclipse 3.5
  • Tomcat 6.0.22
  • Jersey 1.1.2

這裡提供Jersey套件官網下載連結: 點此下載

首先建立一個Dynamic Web Project,假設我這邊命名RestfulDemo

將底下四個Jersey的jar檔放到classpath下,也就是WEB-INF/lib/下,在下試過,缺一不可,會有Exception

asm-3.1.jar

jersey-core-1.1.2-ea.jar

jersey-server-1.1.2-ea.jar

jsr311-api-1.1.jar

設定web.xml,加上下面設定

ServletAdaptor

com.sun.jersey.server.impl.container.servlet.ServletAdaptor

1

ServletAdaptor

/resource/*

建立下面Rest程式

package demo.rest;

import javax.ws.rs.GET;

import javax.ws.rs.Path;

import javax.ws.rs.Produces;

@Path(“/helloworld”) // sets the path for this service

public class HelloRS {

@GET

@Produces(“text/html”) // content type to output

public String getClichedMessage() {

return “Hello World”;

}

}

Delpoy到Tomcat,並用Browser到下列網址,Enjoy : )

http://127.0.0.1:8080/RestfulDemo/resource/helloworld

至於Restful程式撰寫教學,個人覺得,Sun寫的RESTful Web Services Developer’s Guide寫的蠻不錯的,簡單易懂,還蠻容易入門的。

原文章出處:

http://www.dotblogs.com.tw/rockywang/archive/2009/10/22/11224.aspx

沒有留言: