jersey mvc jsp freemarker with embedded tomcat
1.0.0
這是一個簡單的應用程序,旨在使用嵌入式的tomcat和Jersey創建休息完整的Web服務,用於配置信息使用YML文件。該應用程序還使用JSP和FreeMarker模板通過REST API調用渲染。它主要集中於將對象實例註冊到ResourceConfig而不是註冊類。
應用程序創建了兩個tomcat上下文路徑。一個用於休息的完整API終點為/api ,另一個用於網頁的是/webapp ,它直接從Tomcat WebApp Base Directory Location /src/main/webapp加載。
File base = new File("src/main/webapp");
Context context = tomcat.addContext("/api", base.getAbsolutePath());
tomcat.addWebapp(null, "/webapp", base.getAbsolutePath());
此應用程序使用YML文件進行應用程序配置。首先讀取Configuration.yml文件,然後將配置傳遞給應用程序。
#configuration.yml
#setting server configuration
server:
port: 9292
#configure user name
user: "eigenharsha"
ResourceConfig是JAX-RS應用程序類的擴展,但它提供了一些註冊方法,以使註冊資源和提供商更加友好。此應用程序使用public ResourceConfig register(Object component)通過類實例註冊的資源。
JSPMVCFeature和FreeMarkerMvcFeature用於JSP和FreeMarker模板,HTML/Angular/JavaScript直接加載在Web瀏覽器中。
final ResourceConfig config = new ResourceConfig()
.packages(Main.class.getPackage().getName())
.register(new Resource(new Core(), configuration)) // create instance of Resource and dynamically register
.register(JspMvcFeature.class) // register jspMVC
.register(FreemarkerMvcFeature.class) // register FreemarkerMVC
.property(ServletProperties.FILTER_FORWARD_ON_404, true);
return new ServletContainer(config);
可以在此處提供一個用於構建資源的程序化API的深入教程。
這個項目需要毛門。
$ git clone https://github.com/eigenharsha/jersey-mvc-jsp-freemarker-with-embedded-tomcat.git
$ cd jersey-mvc-jsp-freemarker-with-embedded-tomcat
$ mvn clean install
我創建了一個用於在Heroku上部署的Procfile 。在此處提供了在Heroku上部署的深入教程。
$ web: sh target/bin/webapp
部署此應用程序時,我們將能夠訪問此REST服務
| 資源 | 端點 |
|---|---|
| 動態創建的資源調用 | http:// localhost:9292/api/資源 |
| 從YML文件獲取用戶信息 | http:// localhost:9292/api/resource/用戶 |
| 自由標記模板 | http:// localhost:9292/api/resource/freemarker |
| JSP模板 | http:// localhost:9292/api/resource/jsp |
| HTML頁面 | http:// localhost:9292/webapp/index.html |
問題或建議?請隨時通過[email protected]給我發送電子郵件