简体   繁体   English

使用Guice-servlet / Jetty / Jersey的轻量级Java Web堆栈 - 一些问题

[英]Light Java web stack using Guice-servlet/Jetty/Jersey - some questions

I'm thinking about developing a new web application using "light" components and not a full stack framework. 我正在考虑使用“轻量级”组件而不是完整的堆栈框架来开发新的Web应用程序。

This article is my main inspiration! 这篇文章是我的主要灵感!

Jetty : The web server. Jetty :Web服务器。 I'll probably use an embedabble version for development but with an option to export the application as a .war and use an external Jetty server for the production environment. 我可能会使用embedabble版本进行开发,但可以选择将应用程序导出为.war并使用外部Jetty服务器进行生产环境。

Guice/Guice-Servlet : For Dependency injection and for the servlet mapping + filters. Guice / Guice-Servlet :用于依赖注入和servlet映射+过滤器。

Jersey : For the routing + request/response json (de)serialization when required. Jersey :用于路由+请求/响应json(de)序列化时的需要。

An important note : I'm aware some people will use this kind of stack with Jersey as a web services layer only, and will use a Javascript framework (Backbone, AngularJS, etc.) to consume those services and do most of the presentation logic in Javascript. 一个重要的注意事项:我知道有些人会将这种堆栈与Jersey一起用作Web服务层 ,并将使用Javascript框架(Backbone,AngularJS等)来使用这些服务并执行大部分表示逻辑在Javascript中。 I'm not ready yet for this kind of client stuff. 我还没有为这种客户端做好准备。 I still prefere to use JSPs and been able to send plain HTML to clients that have javascript disabled. 我仍然喜欢使用JSP,并且能够将纯HTML发送到禁用了javascript的客户端。

So, my questions : 所以,我的问题:

  • What is the best way to manage forms using Jersey? 使用Jersey管理表单的最佳方法是什么? With Spring MVC (that I used on other projects) there is this concept of "backing objects" where the submitted POST data is automatically binded to a backing object that is then easy to play with. 使用Spring MVC(我在其他项目中使用),有一个“支持对象”的概念,其中提交的POST数据自动绑定到一个易于使用的支持对象。 Is there something similar with Jersey? 泽西有类似的东西吗?

  • I like all my routes to be defined in one specific routes file , not everywhere as @Path annotations which are, in my opinion, harder to manage. 我喜欢在一个特定的路由文件中定义所有路由,而不是像@Path注释那样在我看来更难管理。 I'm pretty sure Jersey requires the use of those hardcoded JAX-RS's @Path annotations and doesn't allow an external routes configuration system, is that correct? 我很确定Jersey要求使用那些硬编码的JAX-RS的@Path注释并且不允许外部路由配置系统,这是正确的吗? Do you see any way I could centralize all routes with Jersey then? 您是否认为我可以用泽西岛集中所有路线?

  • I like the concept of reverse routing (like Play framework provides, for instance). 我喜欢反向路由的概念(例如Play框架提供)。 And, again, I don't think Jersey can provide that functionality, is that correct? 而且,我认为泽西岛不能提供这种功能,这是正确的吗?

  • Considering my previous questions, maybe Jersey is not the right technology to use? 考虑到我以前的问题,也许Jersey不适合使用? Do you know of others libraries I could use for the routing part in my stack? 你知道我可以用于堆栈中的路由部分的其他库吗?

  • Any other suggestions/tips for this kind of light Java web stack? 有关这种轻量级Java Web堆栈的任何其他建议/提示吗?

UPDATE : 更新:

I'm currently looking at UrlRewriteFilter for the routing part. 我目前正在寻找UrlRewriteFilter作为路由部分。

I'm also looking at the ActiveWeb framework , which is a "full stack" framework, but seems light and also seems to provide some functionalities I'm looking for : centralized routing config and reverse-routing. 我也在看ActiveWeb框架 ,它是一个“完整堆栈”框架,但看起来很轻,似乎也提供了一些我正在寻找的功能:集中式路由配置和反向路由。

To explain some terms. 解释一些术语。 Guice and Spring solving same problem domain, which is dependency injection. Guice和Spring解决相同的问题域,即依赖注入。 So, using Guice and SpringMVC at the same time is somehow not possible or at least contrary. 因此,同时使用Guice和SpringMVC在某种程度上是不可能的,或者至少是相反的。

To difference between Guice and Spring, well said: 为了区分Guice和Spring,说得好:

Steep! 陡! Closer to bloody impossible. 更接近血腥不可能。 I think Guice is like taking a girl home for the night. 我认为Guice就像是把一个女孩带回家过夜。 Spring is certain marriage, and if not careful, painful divorce. 春天是一定婚姻,如果不小心,痛苦离婚。 – Spider Oct 7 '11 at 16:25 - 蜘蛛2011年10月7日16:25

Guice is indeed very lightweight DI framework. Guice确实是非常轻量级的DI框架。 But there is not support for routing and templating. 但是不支持路由和模板。 You have to do it by yourself trough binding servlets and using tempaltes engine by your own. 你必须自己通过绑定servlet并使用你自己的tempaltes引擎来完成它。 Or you can use Sitebricks . 或者您可以使用Sitebricks You can put all routing into SitebrickModule configuration method like this: 您可以将所有路由放入SitebrickModule配置方法,如下所示:

public class MyAppConfig extends SitebricksModule {
@Override
protected void configureSitebricks() {
    at("/movies").show(MoviesPage.class); // basic page
    at("/actors").serve(ActorsPage.class); // service
    embed(SoundtrackPage.class).as("Soundtrack"); // brick
}

} }

Sitebricks also support several tempalting system: MVEL, Freemarker,... Sitebricks还支持几种模板系统:MVEL,Freemarker,......

Also, you can easily build up your REST services for javascript usage: 此外,您可以轻松地为javascript用法构建REST服务:

Reply<Product> view() {
  return Reply.with(new Product("Anti-ageing cure"))
              .as(Json.class);
}

Give it a try. 试试看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM