Package org.apache.juneau.rest.client
Interface BodyConverter<T>
- Type Parameters:
T- The type this converter handles.
public interface BodyConverter<T>
Converts a Java object passed to
RestRequest.body(Object) into a TransportBody.
A list of converters is consulted in order; the first converter whose canConvert(Object) returns
true wins. The default converter list (applied automatically by RestClient) handles
the common cases:
HttpBody— passed through directly (streaming preferred over buffering)InputStream— wrapped in a streaming bodybyte[]— wrapped in a byte-array bodyFile— wrapped in a file body
Custom converters are registered on the builder and are prepended to the default list
(checked before the defaults). Use the of(Class, ThrowingConverter) factory for concise lambdas:
Example:
RestClient
See Also:
- Since:
- 9.2.1
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceA checked function used byof(Class, ThrowingConverter). -
Method Summary
Modifier and TypeMethodDescriptionbooleancanConvert(Object value) Returnstrueif this converter can handle the given value.Converts the value to aTransportBody.static <T> BodyConverter<T>of(Class<T> type, BodyConverter.ThrowingConverter<T> converter) Creates aBodyConverterfrom a type predicate and a converting lambda.
-
Method Details
-
canConvert
Returnstrueif this converter can handle the given value.- Parameters:
value- The value passed toRestRequest.body(Object). May benull .- Returns:
trueifconvert(Object)should be called with this value.
-
convert
Converts the value to aTransportBody.- Parameters:
value- The value. Nevernull (converters are only called whencanConvert(java.lang.Object)returns true).- Returns:
- The transport body. Never
null . - Throws:
IOException- If conversion fails.
-
of
Creates aBodyConverterfrom a type predicate and a converting lambda.The converter matches any value that is an instance of the given type.
- Type Parameters:
T- The type to handle.- Parameters:
type- The type to match viainstanceof. Must not benull .converter- The conversion function. Must not benull .- Returns:
- A new converter. Never
null .
-