Class SerializerBody
- All Implemented Interfaces:
HttpBody
HttpBody that serializes a POJO straight to the transport output stream.
The next-generation analog of the classic SerializedEntity: rather than pre-serializing the POJO to an
in-memory String/byte[] and wrapping that, writeTo(OutputStream) runs the supplied
Serializer directly against the wire output stream, so large payloads stream out without full in-memory
materialization.
The body is repeatable: each writeTo(OutputStream) re-serializes the value, so
it can be safely resent (e.g. for a future auto-retry). getContentLength() returns -1 (the
transport uses chunked transfer encoding).
See Also:
- Since:
- 9.2.1
-
Method Summary
Modifier and TypeMethodDescriptionlongReturns the content length in bytes, or-1if unknown.Returns the MIME content type of this body, e.g.booleanReturnstrueif this body can be written more than once.static SerializerBodyof(Serializer serializer, Object value) Creates aSerializerBodyusing the serializer's own media type.static SerializerBodyof(Serializer serializer, Object value, String contentType) Creates aSerializerBodywith an explicit content type.voidwriteTo(OutputStream out) Writes the body content to the given output stream.
-
Method Details
-
of
Creates aSerializerBodyusing the serializer's own media type.- Parameters:
serializer- The serializer that writes the POJO. Must not benull .value- The POJO to serialize. May benull .- Returns:
- A new instance. Never
null .
-
of
Creates aSerializerBodywith an explicit content type.- Parameters:
serializer- The serializer that writes the POJO. Must not benull .value- The POJO to serialize. May benull .contentType- The MIME content type. May benull .- Returns:
- A new instance. Never
null .
-
getContentType
Description copied from interface:HttpBodyReturns the MIME content type of this body, e.g."application/json; charset=UTF-8".- Specified by:
getContentTypein interfaceHttpBody- Returns:
- The content type, or
null if unknown.
-
getContentLength
Description copied from interface:HttpBodyReturns the content length in bytes, or-1if unknown.Implementations that buffer content may return the exact length; streaming implementations should return
-1to cause the transport to use chunked transfer encoding.- Specified by:
getContentLengthin interfaceHttpBody- Returns:
- The content length, or
-1if unknown.
-
writeTo
Description copied from interface:HttpBodyWrites the body content to the given output stream.Implementations must not close
out.- Specified by:
writeToin interfaceHttpBody- Parameters:
out- The stream to write to. Nevernull .- Throws:
IOException- If an I/O error occurs.
-
isRepeatable
Description copied from interface:HttpBodyReturnstrueif this body can be written more than once.Streaming bodies backed by a one-shot
InputStreamshould returnfalse. Buffered bodies (byte arrays, strings, files) should returntrue.- Specified by:
isRepeatablein interfaceHttpBody- Returns:
trueif repeatable.
-