ServletModule¶
By default, GuiceFilter is registered for both application and admin contexts:
- ServletModule can be used for filters and servlets declaration
- Request (and session) scope is available in both contexts
Note
Actually, the same GuiceFilter
instance is used for both contexts. This way request scope works properly
on both contexts, and registrations through ServeletModule
are available both context.
See web mappings report.
Example of servlet and filter registration through guice module:
public class WebModule extends ServletModule {
@Override
protected void configureServlets() {
filter("/*").through(MyFilter.class);
serve("/myservlet").with(MyServlet.class);
}
}
GuiceBundle.builder()
.modules(new WebModule())
.build()
Warning
Rest context is mapped to root by default. To change it use configuration file:
server:
rootPath: '/rest/*'
Note
With guice you can map servlets and filters using regexps
Tip
It may be more handy to use web extensions instead of direct registrations. For example, it is the only way to bind servlets in admin context.
Limitations¶
By default, GuiceFilter
is registered with REQUEST
dispatcher type. If you need to use other types use option:
.option(GuiceyOptions.GuiceFilterRegistration, EnumSet.of(REQUEST, FORWARD))
Warning
Note that async servlets and filters can't be used with guice servlet module (and so it is impossible to register GuiceFilter
for ASYNC
type).
Use web installers for such cases.
Warning
GuiceFilter
dispatch all requests for filters and servlets registered by ServletModule
internally and there may be problems combining servlets from ServletModule
and filters in main scope.
Disable ServletModule support¶
Danger
Option is deprecated because request scope will become mandatory for the next guicey version (due to HK2 remove).
If you don't use servlet modules (for example, because web installers cover all needs) you can disable guice servlet modules support:
GuiceBundle.builder()
.noGuiceFilter()
It will:
- Avoid registration of
GuiceFilter
in both contexts - Remove request and session guice scopes support (because no ServletModule registered)
- Prevent installation of any
ServletModule
(error will be thrown indicating duplicate binding) HttpServletRequest
andHttpServletResponse
still may be injected in resources withProvider
(but it will not be possible to use such injections in servlets, filters or any other place)
Disabling saves about ~50ms of startup time.