简体   繁体   中英

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.

This article is my main inspiration!

Jetty : The web server. 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.

Guice/Guice-Servlet : For Dependency injection and for the servlet mapping + filters.

Jersey : For the routing + request/response json (de)serialization when required.

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. 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.

So, my questions :

  • What is the best way to manage forms using 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. 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. 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? 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). 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? 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?

UPDATE :

I'm currently looking at UrlRewriteFilter for the routing part.

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.

To explain some terms. Guice and Spring solving same problem domain, which is dependency injection. So, using Guice and SpringMVC at the same time is somehow not possible or at least contrary.

To difference between Guice and Spring, well said:

Steep! Closer to bloody impossible. I think Guice is like taking a girl home for the night. Spring is certain marriage, and if not careful, painful divorce. – Spider Oct 7 '11 at 16:25

Guice is indeed very lightweight DI framework. 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. Or you can use Sitebricks . You can put all routing into SitebrickModule configuration method like this:

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,...

Also, you can easily build up your REST services for javascript usage:

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

Give it a try.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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