I am creating a webapp sample using Guice servlets and websocket in tomcat, now when the goose filter is used, websocket stops working
Basic information:
In my web.xml, I initialized the Guiceservlet with a GuiceBasedListener
<web-app> <listener> <listener-class>test.GuiceBasedListener</listener-class> </listener> <filter> <filter-name>guiceFilter</filter-name> <filter-class>com.google.inject.servlet.GuiceFilter</filter-class> </filter> <filter-mapping> <filter-name>guiceFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
GuieBasedListener Code that binds the entire request /* to MyDispatcher
public class GuiceBasedListener extends GuiceServletContextListener { protected Injector getInjector() { return Guice.createInjector( new ServletModule() { @Override protected void configureServlets() { bind(MyDispatcher.class).asEagerSingleton(); serve("/*").with(MyDispatcher.class);
MyDispatcher that just responds with a string
public class MyDispatcher extends HttpServlet { @Inject private Injector injector; public MyDispatcher() {} public void service(ServletRequest req, ServletResponse resp) throws IOException, ServletException { resp.getOutputStream().print("SUCCESS:" + req); } }
I also have @ServerEndPoint for Websocket
@ServerEndpoint(value = "/websocket/chat2") public class WebSocket{ .... @OnOpen public void start(Session session) { System.out.println("Staring:"+this); } .... }
Remarks:
- Now, if I run the application and hit http: // app: 8080 / test , it returns
SUCCESS - But if I try to connect to websocket using ws: // app: 8080 / websocket / chat2, it does not work.
Now, if I comment on serve("/*").with(MyDispatcher.class); basically, if we disable guice routing, the website will start working
If I disable guice-servlet, but add the servlet mapping in web.xml as below, websocket still works
< servlet-mapping > < servlet-name > HelloWorld< / servlet-name > < url-pattern > /* < / url-pattern > < / servlet-mapping >
What am I missing or doing wrong?
EDIT:
Surveillance-Conti:
- What I did was define a simple filter that simply responds with
FILTER .
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { response.getOutputStream().print("FILTER"); }
and changed my web.xml to
<web-app> <filter> <filter-name>myFilter</filter-name> <filter-class>test.MyFilter</filter-class> </filter> <filter-mapping> <filter-name>myFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
Now hit http://localhost:8080/app/x return FILTER as expected. But trying to connect to websocket fails, as the request shows something like this. I also noticed that as I change the MyFilter line, we return the length of the content in the responses, which means that the request reached MyFilter before tomcat processed it for websocket.

I changed web.xml below, and guice and websocket now work fine. Therefore, I believe Guice does not honor WsFilter registered after GuiceFilter
<filter> <filter-name>myFilter</filter-name> <filter-class>org.apache.tomcat.websocket.server.WsFilter</filter-class> </filter> <filter-mapping> <filter-name>myFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>guiceFilter</filter-name> <filter-class>com.google.inject.servlet.GuiceFilter</filter-class> </filter> <filter-mapping> <filter-name>guiceFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
TOMCAT 8.0, Window 7, Java 1.7, Guice 4.0, Guice-servlet-4.0