Question

I'm using Guice to route requests in my web app, and I'd like to modularize the routing of some of the URL patterns I'll be handling. Ideally, I'd like to be able to do something like this in my ServletModule:

delegate("/foo/bar/*").to(SomeOtherServletModule.class);

// in SomeOtherServletModule.configureServlets:
serve("/foo/bar/quux").with(Quux.class);

Or even better:

delegatePrefix("/foo/bar/").to(SomeOtherServletModule.class);

// in SomeOtherServletModule.configureServlets:
serve("/quux").with(Quux.class); // prefix removed

Is this possible in Guice? It seems that Guice tries really hard to make the bindings installed by ServletModules a singleton, which is in turn stored who-knows-where by GuiceServletContextListener to be used by GuiceFilter, but I'd like to un-singleton this so I can delegate like this, instead of having everything tightly bound in a single function.

Was it helpful?

Solution

I am the creator of Guice Servlet. This is basically not allowed as you are describing, as we felt it would be confusing for modules that are not intended to be used this way.

For example, many modules contribute filters that register at "/*" to provide some interception functionality (such as transactions). These could accidentally stop working if you auto-prefixed them. Given that servlet modules can be transitively installed, this is not as simple as being vigilant about one piece of code. Also, with regex bindings (example: /\.html$/), how does one handle prefixes? Do we support them (difficult problem)? Or do we simply register regex bindings normally and make a surprising exception for them?

Considering that what you're asking for is trivially achieved with a constructor in SomeOtherServletModule("/myprefix") that knows how to map itself correctly, we decided not to provide this functionality.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top