Mapping WebSocketEndpoints in a web.xml file
jakarta-ee, java, java-ee-7, jetty, web-applications
Solution
Assuming you have annotated `game.WebSocketEndpoint` using the JSR-356 techniques ...
Example:
package game;
import javax.websocket.server.ServerEndpoint
@ServerEndpoint("/game")
public class WebSocketEndpoint {
}
Then you have to do the following...
- Use Jetty 9.1+
- Enable the 'websocket' module (add `--module=websocket` to your `start.ini` or command line)
That will enable the websocket server classes + annotation scanning for websocket endpoints.
Note: that JSR-356 isn't meant to be mapped via the deployment descriptor (`web.xml`).
However, you can programmatically map your endpoints using one of the following techniques:
- Create a `javax.servlet.ServletContextListener` that manually adds endpoints via the `javax.websocket.server.ServerContainer` (see below for how)
- Create a `javax.servlet.ServerContainerInitializer` that manually adds endpoints via the `javax.websocket.server.ServerContainer` (see below for how)
- Create a `javax.websocket.server.ServerAppliationConfig` that returns the endpoints that you want to add.
Note: technique #2 and #3 both require class scanning for annotations (slow startup). technique #1 is fast startup.
How to Manually Add Endpoints
// Get a reference to the ServerContainer
javax.websocket.server.ServerContainer ServerContainer =
(javax.websocket.server.ServerContainer)
servletContext.getAttribute("javax.websocket.server.ServerContainer");
// Add endpoint manually to server container
serverContainer.addEndpoint(game.WebSocketEndpoint.class);
Problem
I am trying to develop a Java EE 7 web application that uses a websocket endpoint and deploy it on a Jetty server. The application has the following structure: ``` Game/ src/ main/ java/ game/ WebSocketEndpoint.java webapp/ index.html scripts/ variousjavascriptstuff.js WEB-INF/ beans.xml web.xml ``` In the beans.xml file: ``` <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd" bean-discovery-mode="annotated"> ``` WebSocketEndpoint is annotated properly and works fine with Netbeans/Glassfish4, however, the application must be deployed on a Jetty server. So, my question - How do I map the websocket endpoint to the URL /game in the web.xml file? I have found a number of examples for mapping servlets, but I do not think that this will work for a server endpoint. Or, is there a way to write a web.xml file for Jetty so that it automatically discovers ll annotated classes/methods (similar to the above beans.xml)