The @ServerEndpoint annotation marks a Java class as a WebSocket server endpoint and binds it to a URI path. It comes from the Jakarta WebSocket API (formerly Java API for WebSocket, JSR 356), so any compliant container such as Tomcat, Jetty, Undertow or a Jakarta EE server discovers the class and accepts connections at that path.
Inside the class, lifecycle annotations (@OnOpen, @OnMessage, @OnClose, @OnError) mark the methods that react to connection events, with no interface to implement.
The package is jakarta.websocket since Jakarta EE 9 and Spring Boot 3; older code uses javax.websocket.
A client connects to ws://localhost:8080/chat/general. The value general is injected into room through @PathParam. Every message is relayed to the other sessions in the same room.
| Attribute | Purpose |
|---|---|
value | URI or URI template, for example /chat/{room} |
encoders | Classes that convert Java objects to text or binary messages |
decoders | Classes that convert incoming messages to Java objects |
subprotocols | Supported WebSocket subprotocols for negotiation |
configurator | A ServerEndpointConfig.Configurator for custom instance creation, handshake checks or origin validation |
The path must start with /. Templates such as {room} are matched against path segments and read with @PathParam.
@OnOpen runs after the handshake. It can receive the Session, an EndpointConfig and path parameters.@OnMessage runs for each text, binary or pong message. The first parameter selects the message type: String, byte[], ByteBuffer, a decoded object, or PongMessage. A return value is sent back to the sender.@OnClose runs when the connection ends and receives a CloseReason with the close code.@OnError runs on protocol or handler exceptions, and you should clean up state there.By default the container creates one endpoint instance per connection, so instance fields belong to one client. Shared state such as the session set above must be static or injected and thread safe.
In a standalone servlet container or Jakarta EE server, the annotated class is found by classpath scanning when the application is deployed, so no extra configuration is needed.
In Spring Boot with embedded Tomcat, scanning does not apply. Register a ServerEndpointExporter and make the endpoint a Spring bean if you want dependency injection:
Because the container instantiates endpoint classes itself, plain @Autowired fields are not populated unless the class is a Spring component and the exporter is used. For new Spring projects, prefer Spring's own WebSocketHandler or STOMP messaging, which integrate with security and dependency injection natively.
Session.getBasicRemote() blocks the calling thread until the frame is sent. Session.getAsyncRemote() returns immediately. Do not call sendText concurrently on the same session from several threads, because many containers throw IllegalStateException or interleave frames. Serialise sends per session, for example with a lock or a queue, or use the async API with a single sender.
Origin header in a Configurator.checkOrigin override, since browsers do not apply the same-origin policy to WebSockets.modifyHandshake can copy the principal into user properties.wss:// through TLS in production.session.setMaxIdleTimeout and setMaxTextMessageBufferSize to avoid memory abuse.@ServerEndpoint defines a server-side endpoint that waits for connections. @ClientEndpoint marks a class that opens connections to a server through WebSocketContainer.connectToServer.
No. By default one instance is created per connection. Use a custom Configurator to change instantiation.
The ServerEndpointExporter bean is usually missing, so annotated classes are never registered.
Use jakarta.websocket on Jakarta EE 9 and later, including Spring Boot 3 and newer. javax.websocket applies to older servers only.
@ServerEndpoint turns a class into a WebSocket server endpoint at a given path, with lifecycle methods for open, message, close and error events. Register it through the container (or ServerEndpointExporter in Spring Boot), keep shared state thread safe, check the origin, authenticate during the handshake, and consider Spring's STOMP support when you need broadcasting and security integration.