Java Reference
In-Depth Information
for an endpoint class, but it must be a nonabstract concrete class with a public no-argument
constructor. Dependency injection is performed after the class is created.
@ServerEndpoint
The
@ServerEndpoint
annotation is the annotation that marks a class as being a We-
bSocket endpoint. The container scans the class path at startup looking for classes with this
annotation. Besides identifying the class as a WebSocket endpoint, it also configures the
URI for the endpoint, as well as the encoders and decoders to be used when processing a
message. The annotation is defined as follows:
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = {ElementType.TYPE})
public @interface ServerEndpoint {
public String value();
public String[] subprotocols() default {};
public Class<? extends Decoder>[] decoders() default {};
public Class<? extends Encoder>[] encoders() default {};
public Class<? extends ServerEndpointConfig.Configurator> configurator()
default ServerEndpointConfig.Configurator.class;
}
The parameters to the annotation configure the endpoint:
•
value—
The URI of the endpoint
•
subprotocols—
Ordered list of application-level protocols
•
decoders—
Ordered list of
javax.websocket.Decoder
subclasses
•
encoders—
Ordered list of
javax.websocket.Encoder
subclasses
•
configurator—
Custom to further configure the endpoint
Only the
value
parameter, specifying the URI of the endpoint, is required. All other para-
meters are optional. The URI is relative to the root of the WebSocket container. Because
the WebSocket container is most often used from a web application container, the root is
the same as the web application container. To get a better understanding of this annotation,
let's look at an example from
BulletinService
from ActionBazaar in the next listing.