Package io.micronaut.json.body
Class JsonMessageHandler<T>
java.lang.Object
io.micronaut.json.body.JsonMessageHandler<T>
- Type Parameters:
T- The type to read/write
- All Implemented Interfaces:
MessageBodyHandler<T>,MessageBodyReader<T>,MessageBodyWriter<T>,ResponseBodyWriter<T>,CustomizableJsonHandler
@Order(-10)
@Singleton
@ProducesJson
@ConsumesJson
public final class JsonMessageHandler<T>
extends Object
implements MessageBodyHandler<T>, CustomizableJsonHandler, ResponseBodyWriter<T>
Body handler for JSON.
- Since:
- 4.0.0
- Author:
- Jonas Konrad
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe JSON handler should be preferred if for any type. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncreateSpecific(@NonNull Argument<T> type) Prepare aMessageBodyWriterthat will write the given type.customize(JsonFeatures jsonFeatures) Get the json mapper used by this handler.booleanisReadable(@NonNull Argument<T> type, MediaType mediaType) Is the type readable.booleanisWriteable(@NonNull Argument<T> type, MediaType mediaType) Is the type writeable.read(@NonNull Argument<T> type, MediaType mediaType, @NonNull Headers httpHeaders, @NonNull ByteBuffer<?> byteBuffer) Reads an object from the given byte buffer.read(@NonNull Argument<T> type, MediaType mediaType, @NonNull Headers httpHeaders, @NonNull InputStream inputStream) Reads an object from the given byte buffer.writePiece(@NonNull ByteBodyFactory bodyFactory, @NonNull HttpRequest<?> request, @NonNull HttpResponse<?> response, @NonNull Argument<T> type, @NonNull MediaType mediaType, T object) Write a piece of a larger response, e.g.voidwriteTo(Argument<T> type, @NonNull MediaType mediaType, T object, MutableHeaders outgoingHeaders, @NonNull OutputStream outputStream) Writes an object to the given output stream.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.micronaut.http.body.MessageBodyWriter
isBlocking, writeToMethods inherited from interface io.micronaut.http.body.ResponseBodyWriter
write
-
Field Details
-
ORDER
public static final int ORDERThe JSON handler should be preferred if for any type.- See Also:
-
-
Constructor Details
-
JsonMessageHandler
-
-
Method Details
-
getJsonMapper
Get the json mapper used by this handler.- Returns:
- The mapper
-
isReadable
Description copied from interface:MessageBodyReaderIs the type readable.- Specified by:
isReadablein interfaceMessageBodyReader<T>- Parameters:
type- The typemediaType- The media type, can benull- Returns:
- True if is readable
-
createSpecific
Description copied from interface:MessageBodyWriterPrepare aMessageBodyWriterthat will write the given type. This can be used for precomputing some route data.- Specified by:
createSpecificin interfaceMessageBodyWriter<T>- Parameters:
type- The type- Returns:
- The closure
-
read
public T read(@NonNull @NonNull Argument<T> type, MediaType mediaType, @NonNull @NonNull Headers httpHeaders, @NonNull @NonNull ByteBuffer<?> byteBuffer) throws CodecException Description copied from interface:MessageBodyReaderReads an object from the given byte buffer.- Specified by:
readin interfaceMessageBodyReader<T>- Parameters:
type- The type being decoded.mediaType- The media type, can benullhttpHeaders- The HTTP headersbyteBuffer- The byte buffer- Returns:
- The read object or
null - Throws:
CodecException- If an error occurs decoding
-
read
public T read(@NonNull @NonNull Argument<T> type, MediaType mediaType, @NonNull @NonNull Headers httpHeaders, @NonNull @NonNull InputStream inputStream) throws CodecException Description copied from interface:MessageBodyReaderReads an object from the given byte buffer.- Specified by:
readin interfaceMessageBodyReader<T>- Parameters:
type- The type being decoded.mediaType- The media type, can benullhttpHeaders- The HTTP headersinputStream- The input stream- Returns:
- The read object or
null - Throws:
CodecException- If an error occurs decoding
-
isWriteable
Description copied from interface:MessageBodyWriterIs the type writeable.- Specified by:
isWriteablein interfaceMessageBodyWriter<T>- Parameters:
type- The typemediaType- The media type, can benull- Returns:
- True if is writable
-
writeTo
public void writeTo(Argument<T> type, @NonNull @NonNull MediaType mediaType, T object, MutableHeaders outgoingHeaders, @NonNull @NonNull OutputStream outputStream) throws CodecException Description copied from interface:MessageBodyWriterWrites an object to the given output stream.- Specified by:
writeToin interfaceMessageBodyWriter<T>- Parameters:
type- The typemediaType- The media typeobject- The object to writeoutgoingHeaders- The HTTP headersoutputStream- The output stream- Throws:
CodecException- If an error occurs decoding
-
writePiece
@NonNull public @NonNull CloseableByteBody writePiece(@NonNull @NonNull ByteBodyFactory bodyFactory, @NonNull @NonNull HttpRequest<?> request, @NonNull @NonNull HttpResponse<?> response, @NonNull @NonNull Argument<T> type, @NonNull @NonNull MediaType mediaType, T object) throws CodecException Description copied from interface:ResponseBodyWriterWrite a piece of a larger response, e.g. when writing a Publisher or a part of a multipart response. In this case, response headers cannot be modified.- Specified by:
writePiecein interfaceResponseBodyWriter<T>- Parameters:
bodyFactory- The buffer factoryrequest- The requestresponse- The response this piece is part oftype- The type of this piecemediaType- The media type of this pieceobject- The piece to write- Returns:
- The response bytes
- Throws:
CodecException- If an error occurs encoding
-
customize
- Specified by:
customizein interfaceCustomizableJsonHandler
-