Class ConvertingEncoderDecoderSupport<T,M>
- java.lang.Object
- org.springframework.web.socket.adapter.standard.ConvertingEncoderDecoderSupport<T,M>
- Type Parameters:
T
- the type being converted to (for Encoder) or from (for Decoder)M
- the WebSocket message type (String
orByteBuffer
)
- Direct Known Subclasses:
ConvertingEncoderDecoderSupport.BinaryDecoder
,ConvertingEncoderDecoderSupport.BinaryEncoder
,ConvertingEncoderDecoderSupport.TextDecoder
,ConvertingEncoderDecoderSupport.TextEncoder
public abstract class ConvertingEncoderDecoderSupport<T,M> extends Object
Base class that can be used to implement a standardEncoder
and/orDecoder
. It provides encode and decode method implementations that delegate to a SpringConversionService
.By default, this class looks up a
ConversionService
registered in theactive ApplicationContext
under the name'webSocketConversionService'
. This works fine for both client and server endpoints, in a Servlet container environment. If not running in a Servlet container, subclasses will need to override thegetConversionService()
method to provide an alternative lookup strategy.Subclasses can extend this class and should also implement one or both of
Encoder
andDecoder
. For convenienceConvertingEncoderDecoderSupport.BinaryEncoder
,ConvertingEncoderDecoderSupport.BinaryDecoder
,ConvertingEncoderDecoderSupport.TextEncoder
andConvertingEncoderDecoderSupport.TextDecoder
subclasses are provided.Since JSR-356 only allows Encoder/Decoder to be registered by type, instances of this class are therefore managed by the WebSocket runtime, and do not need to be registered as Spring Beans. They can, however, by injected with Spring-managed dependencies via
@Autowire
.Converters to convert between the
type
andString
orByteBuffer
should be registered.
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ConvertingEncoderDecoderSupport.BinaryDecoder<T>
A binaryjavax.websocket.Encoder
that delegates to Spring's conversion service.static class
ConvertingEncoderDecoderSupport.BinaryEncoder<T>
A binaryjavax.websocket.Encoder
that delegates to Spring's conversion service.static class
ConvertingEncoderDecoderSupport.TextDecoder<T>
A Textjavax.websocket.Encoder
that delegates to Spring's conversion service.static class
ConvertingEncoderDecoderSupport.TextEncoder<T>
A textjavax.websocket.Encoder
that delegates to Spring's conversion service.
Constructor Summary
Constructors Constructor Description ConvertingEncoderDecoderSupport()
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description T
decode(M message)
void
destroy()
M
encode(T object)
protected ApplicationContext
getApplicationContext()
Returns the activeApplicationContext
.protected ConversionService
getConversionService()
Strategy method used to obtain theConversionService
.protected TypeDescriptor
getMessageType()
Returns the websocket message type.protected TypeDescriptor
getType()
Returns the type being converted.void
init(EndpointConfig config)
boolean
willDecode(M bytes)
Constructor Detail
ConvertingEncoderDecoderSupport
public ConvertingEncoderDecoderSupport()
Method Detail
init
public void init(EndpointConfig config)
destroy
public void destroy()
- See Also:
Encoder.destroy()
,Decoder.destroy()
getConversionService
protected ConversionService getConversionService()
Strategy method used to obtain theConversionService
. By default this method expects a bean named'webSocketConversionService'
in theactive ApplicationContext
.- Returns:
- the
ConversionService
(never null)
getApplicationContext
protected ApplicationContext getApplicationContext()
Returns the activeApplicationContext
. Be default this method obtains the context viaContextLoader.getCurrentWebApplicationContext()
, which finds the ApplicationContext loaded viaContextLoader
typically in a Servlet container environment. When not running in a Servlet container and not usingContextLoader
, this method should be overridden.- Returns:
- the
ApplicationContext
ornull
getType
protected TypeDescriptor getType()
Returns the type being converted. By default the type is resolved using the generic arguments of the class.
getMessageType
protected TypeDescriptor getMessageType()
Returns the websocket message type. By default the type is resolved using the generic arguments of the class.
encode
public M encode(T object) throws EncodeException
- Throws:
EncodeException
- See Also:
Encoder.Text.encode(Object)
,Encoder.Binary.encode(Object)
willDecode
public boolean willDecode(M bytes)
decode
public T decode(M message) throws DecodeException