
Java 與 REST 的邂逅(三)淺談 Jersey MVC
Java 與 REST 的邂逅(二)JAX-RS 核心 Annotation << 前情 簡介前面兩篇介紹以Jersey來做RESTful Web Service,Jersey的輕量簡潔但是強大的功能,對於RESTful的應用可以寫得非常簡單漂亮。這時候我們不難會繼續想說用Jersey來寫RESTful Web Application有沒有搞頭,這絕對有搞頭!!! 而且事實上也非常的簡單好用。 在Jersey本身,它定義了一個Viewable的class,當resource method回傳的是Viewable,則代表我們想要把結果轉到View上去執行,這不就是我們常見的MVC pattern嗎?沒錯,在Jersey當中,我們可以把resource method當作controller,在其中根據client request,產生對應的model,再dispatch到view去做呈現。因此如果想要選擇一個比較輕量級的MVC framework,但是又不想用Servlet當作Controller那麼陽春,那Jersey會是一個很好的選擇。 設定Jersey MVC首先你需要加上這兩個Jersey Maven Dependency。Jersey目前提供JSP或是FreeMarker兩種template的實作,在此篇我們就以比較常用的JSP作為例子。 <dependency> <groupId>org.glassfish.jersey.ext</groupId> <artifactId>jersey-mvc</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>org.glassfish.jersey.ext</groupId> <artifactId>jersey-mvc-jsp</artifactId> <version>2.5</version> </dependency> 再來是設定web.xml。根據官網的文件,若要要使用Jersey MVC並且使用JSP當作template,必須在web.xml中明確用Filter的方式來設定Jersey,這個目前我還不清楚原因,官網這樣寫我們就照著做。 <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>Jersey MVC Sample</display-name> <!-- Jersey --> <filter> <filter-name>jersey</filter-name> <filter-class>org.glassfish.jersey.servlet.ServletContainer</filter-class> <init-param> <param-name>javax.ws.rs.Application</param-name> <param-value>tw.com.codedata.jersey.MyApplication</param-value> </init-param> <init-param> <param-name>jersey.config.servlet.filter.forwardOn404</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>jersey</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> 再來是在我們的Jersey的Application class中,新增有關MVC的設定。其中JspMvcFeature是用來描述要使用Jersery MVC這個Feature,並且已JSP當作Template Engine。而 package tw.com.codedata.jersey; import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.server.mvc.jsp.JspMvcFeature; public class MyApplication extends ResourceConfig{ public MyApplication(){ packages("tw.com.codedata.jersey.controller"); register(JspMvcFeature.class); property(JspMvcFeature.TEMPLATES_BASE_PATH, "/WEB-INF/jsp"); } } 再來來改寫我們的Resource(Controller) class。在Resource method中我們回傳一個Viewable代表我們想把結果丟到View上做呈現。建構子的第一個參數是Template path,第二個參數是丟給Template的model。注意template path不需要指定 package tw.com.codedata.jersey.controller; import java.util.HashMap; import javax.ws.rs.DefaultValue; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.QueryParam; import org.glassfish.jersey.server.mvc.Viewable; @Path("/hello") public class HelloController { @GET public Viewable sayHello(@QueryParam("name") @DefaultValue("World") String name) { HashMap model = new HashMap(); model.put("name", name); return new Viewable("/hello", model); } } 在Jersey把model丟到JSP後,它會產生一個 <%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <html> <head> </head> <body> Hello, <c:out value="${it.name}" /> </body> </html> 結論Jersey針對RESTful提供了需要的GET,POST,PUT,DELETE等annotation來快速對應method成為一個resource method。並且有各式各樣的 當然,如果您已經對Spring很熟悉了,並且已經大量的使用Spring的功能,你可以選擇使用Spring MVC,這兩個是很類似的東西。但如果你想選擇比較輕量級,必且想使用JAX-RS提供的API,Jersey MVC提供給你的是一個比較輕量但功能也足夠應付大部份情況的解決方案。 |