Servlet 3.0 annotations combined with Guice

I am trying to update an outdated Guice application, and I was wondering if there is any preferred way to do something when considering servlet 3.0 annotations. For example, my application has a FooFilter filter, which is defined in the Guice Module Factory configureServlets () module as follows:

Map<String, String> fooParams = new HashMap<String, String>(); fooParams.put("someParam", "parameter information"); filter("/foo.jsp","/foo/*").through(com.example.filter.FooFilter.class, fooParams); 

Whether the above binding is required or will interfere with the following use of the @WebFilter Servlet 3.0 annotation:

  @Singleton @WebFilter( filterName="FooFilter", urlPatterns={"/foo.jsp", "/foo/*"}, initParams = { @WebInitParam(name="foo", value="Hello "), @WebInitParam(name="bar", value=" World!") }) public class FooFilter implements Filter { etc.... 

Which method is now preferred? Will they mess with each other?

+6
source share
1 answer

I just made a quick project on how Servlet 3.0 support might look. There may be a more elegant way to simply call filter(Filter Class with WebFilter annotation) in the configureServlet method, but this requires an updated right to the guice-servlet module, which is quite difficult to distribute.

Well, what I did was a project on Github: https://github.com/xbaran/guice-servlet3

all you have to do is download and build. It is built on top of Guice 3.0 and works as follows:

 new Servlet3Module() { @Override protected void configureServlets3() { scanFilters(FooFilter.class.getPackage()); } }; 

Servlet3Module extends ServletModule and contains a scanFilters method with the package argument. This method scans the provided package from your class path and tries to register all classes using the WebFilter annotation using the filter() method.

This scan idea is based on Sitebricks (a Sitebricks web framework created by Dhanji R. Prasanna configuration system).

Honestly, I'm just doing a draft, never trying if it works. But I hope it will be so. If you have any problems or questions, just let me know.

PS: support for servlets, listeners, etc. can be added if you want.

0
source

Source: https://habr.com/ru/post/957720/


All Articles