Interface BodyCodec
Registration. Implementations are discovered with ServiceLoader
(through the thread context class loader) once, when an application starts. A codec is selected
by the exact lowercase type/subtype it declares in mediaTypes(): there is no
priority order, and wildcards (application/*), structured-syntax suffix aliases (a codec
for application/json does not serve application/vnd.api+json) and parameters are
not matched, so a codec lists every type it serves. Several codecs may be installed as long as
their media types are disjoint. Two codecs declaring the same media type, or a declaration that
is not an exact type/subtype, fail startup with an IllegalStateException naming
the type and the classes, and the application stays configurable.
Lifecycle and threading. The runtime creates a codec through its public no-argument
constructor, never closes it, and keeps one instance for the application's lifetime. That
instance is shared by every request and may be called from many threads at the same time (on
virtual threads, so a call should not pin a carrier thread for long): implementations must be
thread-safe, must not keep per-request state in fields, and should be immutable after
construction. The runtime reads mediaTypes() once, at startup, and routes by that index;
it does not call supports(String).
This SPI is experimental. Failures must never expose input or parser messages: decoding
reports DecodeException with a safe code, and encoding
failures are server errors. Implementations should not depend on the charset parameter;
the runtime accepts only UTF-8 (or no charset parameter) before calling a codec.
-
Method Summary
Modifier and TypeMethodDescription<T> TDecodes a complete request body.default <T> Tdecode(ByteBuffer content, Class<T> type) Decodes a complete request body from a read-only view of its content.byte[]Encodes a response value synchronously during Context.json or Context.encode.Returns the media types this codec handles.default booleanReports whether this codec handles a media type.
-
Method Details
-
mediaTypes
Returns the media types this codec handles.- Returns:
- non-empty set of lowercase
type/subtypenames without parameters
-
supports
Reports whether this codec handles a media type.- Parameters:
mediaType- lowercasetype/subtypewithout parameters- Returns:
- true if the type is one of
mediaTypes()
-
decode
Decodes a complete request body.- Type Parameters:
T- target type- Parameters:
content- UTF-8 content owned by the codec for the duration of the calltype- target type- Returns:
- decoded value, never null
- Throws:
DecodeException- if the content is malformed or does not match the type
-
decode
Decodes a complete request body from a read-only view of its content. The runtime calls this method, passing a view of the request'sBodyso that the content is not copied for the codec.The default implementation copies the remaining bytes into a new array once and calls
decode(byte[], Class), so codecs that implement only that method keep working. Codecs that can read a buffer directly should override this method. Implementations may move the buffer's position, must not retain the buffer or a view of it after returning, and cannot modify the content because the buffer is read-only.- Type Parameters:
T- target type- Parameters:
content- read-only view of the UTF-8 content, from its position to its limittype- target type- Returns:
- decoded value, never null
- Throws:
DecodeException- if the content is malformed or does not match the type
-
encode
Encodes a response value synchronously during Context.json or Context.encode. The caller keeps the value stable until this method returns. Do not retain the value or references into its object graph. The runtime copies the returned bytes into the response.- Parameters:
value- value to encode, never a String or byte array- Returns:
- encoded UTF-8 bytes, never null
- Throws:
IllegalStateException- if the value cannot be encoded
-