Class RequestContent
The RequestContent
object is the API for accessing the content of an HTTP request.
It can be accessed by passing it as a parameter on your REST Java method:
Example:
Some important methods on this class are:
RequestContent
- Methods for accessing the raw contents of the request content:
- Methods for parsing the contents of the request content:
- Other methods:
See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescription<T> T
Reads the input from the HTTP request parsed into a POJO.<T> T
Reads the input from the HTTP request parsed into a POJO.byte[]
asBytes()
Returns the HTTP content content as a plain string.asHex()
Returns the HTTP content content as a simple hexadecimal character string.Returns the HTTP content content as a simple space-delimited hexadecimal character string.asString()
Returns the HTTP content content as a plain string.cache()
Caches the content in memory for reuse.content
(byte[] value) Sets the contents of this content.encoders
(EncoderSet value) Sets the encoders to use for decoding this content.int
Returns the content length of the content.jakarta.servlet.ServletInputStream
Returns the HTTP content content as anInputStream
.Returns the parser and media type matching the requestContent-Type header.Returns the HTTP content content as aReader
.protected Reader
Same asgetReader()
, but doesn't encapsulate the result in aBufferedReader
;maxInput
(long value) Sets the max input value for this content.Sets the media type of this content.Sets the parser to use for this content.Sets the parsers to use for parsing this content.setSchema
(HttpPartSchema schema) Sets the schema for this content.
-
Constructor Details
-
RequestContent
Constructor.- Parameters:
req
- The request creating this bean.
-
-
Method Details
-
encoders
Sets the encoders to use for decoding this content.- Parameters:
value
- The new value for this setting.- Returns:
- This object.
-
parsers
Sets the parsers to use for parsing this content.- Parameters:
value
- The new value for this setting.- Returns:
- This object.
-
setSchema
Sets the schema for this content.- Parameters:
schema
- The new schema for this content.- Returns:
- This object.
-
maxInput
Sets the max input value for this content.- Parameters:
value
- The new value for this setting.- Returns:
- This object.
-
mediaType
Sets the media type of this content.- Parameters:
value
- The new value for this setting.- Returns:
- This object.
-
parser
Sets the parser to use for this content.- Parameters:
value
- The new value for this setting.- Returns:
- This object.
-
content
Sets the contents of this content.- Parameters:
value
- The new value for this setting.- Returns:
- This object.
-
as
Reads the input from the HTTP request parsed into a POJO.The parser used is determined by the matching
Content-Type header on the request.If type is
null orObject.
, then the actual type will be determined automatically based on the following input:class Type JSON input XML input Return type object "{...}" <object> ...</object> <x type ='object' > ...</x> JsonMap
array "[...]" <array> ...</array> <x type ='array' > ...</x> JsonList
string "'...'" <string> ...</string> <x type ='string' > ...</x> String
number 123 <number> 123</number> <x type ='number' > ...</x> Number
boolean true <boolean> true</boolean> <x type ='boolean' > ...</x> Boolean
null null or blank
or blank<null/> <x type ='null' /> null Refer to POJO Categories for a complete definition of supported POJOs.
Examples:
// Parse into an integer. int content1 =req .getContent().as(int .class );// Parse into an int array. int []content2 =req .getContent().as(int [].class );// Parse into a bean. MyBeancontent3 =req .getContent().as(MyBean.class );// Parse into a linked-list of objects. Listcontent4 =req .getContent().as(LinkedList.class );// Parse into a map of object keys/values. Mapcontent5 =req .getContent().as(TreeMap.class );Notes:
-
If
allowContentParam
init parameter is true, then first looks for&content=xxx
in the URL query string.
- Type Parameters:
T
- The class type to instantiate.- Parameters:
type
- The class type to instantiate.- Returns:
- The input parsed to a POJO.
- Throws:
BadRequest
- Thrown if input could not be parsed or fails schema validation.UnsupportedMediaType
- Thrown if the Content-Type header value is not supported by one of the parsers.InternalServerError
- Thrown if anIOException
occurs.
-
If
-
as
public <T> T as(Type type, Type... args) throws BadRequest, UnsupportedMediaType, InternalServerError Reads the input from the HTTP request parsed into a POJO.This is similar to
as(Class)
but allows for complex collections of POJOs to be created.Examples:
// Parse into a linked-list of strings. List<String>content1 =req .getContent().as(LinkedList.class , String.class );// Parse into a linked-list of linked-lists of strings. List<List<String>>content2 =req .getContent().as(LinkedList.class , LinkedList.class , String.class );// Parse into a map of string keys/values. Map<String,String>content3 =req .getContent().as(TreeMap.class , String.class , String.class );// Parse into a map containing string keys and values of lists containing beans. Map<String,List<MyBean>>content4 =req .getContent().as(TreeMap.class , String.class , List.class , MyBean.class );Notes:
-
Collections must be followed by zero or one parameter representing the value type. -
Maps must be followed by zero or two parameters representing the key and value types. -
If
allowContentParam
init parameter is true, then first looks for&content=xxx
in the URL query string.
- Type Parameters:
T
- The class type to instantiate.- Parameters:
type
- The type of object to create.
Can be any of the following:ClassMeta
,Class
,ParameterizedType
,GenericArrayType
args
- The type arguments of the class if it's a collection or map.
Can be any of the following:ClassMeta
,Class
,ParameterizedType
,GenericArrayType
Ignored if the main type is not a map or collection.- Returns:
- The input parsed to a POJO.
- Throws:
BadRequest
- Thrown if input could not be parsed or fails schema validation.UnsupportedMediaType
- Thrown if the Content-Type header value is not supported by one of the parsers.InternalServerError
- Thrown if anIOException
occurs.
-
-
asString
Returns the HTTP content content as a plain string.Notes:
-
If
allowContentParam
init parameter is true, then first looks for&content=xxx
in the URL query string.
- Returns:
- The incoming input from the connection as a plain string.
- Throws:
IOException
- If a problem occurred trying to read from the reader.
-
If
-
asBytes
Returns the HTTP content content as a plain string.Notes:
-
If
allowContentParam
init parameter is true, then first looks for&content=xxx
in the URL query string.
- Returns:
- The incoming input from the connection as a plain string.
- Throws:
IOException
- If a problem occurred trying to read from the reader.
-
If
-
asHex
Returns the HTTP content content as a simple hexadecimal character string.Example:
0123456789ABCDEF
- Returns:
- The incoming input from the connection as a plain string.
- Throws:
IOException
- If a problem occurred trying to read from the reader.
-
asSpacedHex
Returns the HTTP content content as a simple space-delimited hexadecimal character string.Example:
01 23 45 67 89 AB CD EF
- Returns:
- The incoming input from the connection as a plain string.
- Throws:
IOException
- If a problem occurred trying to read from the reader.
-
getReader
Returns the HTTP content content as aReader
.Notes:
-
If
allowContentParam
init parameter is true, then first looks for&content=xxx
in the URL query string. - Automatically handles GZipped input streams.
- Returns:
- The content contents as a reader.
- Throws:
IOException
- Thrown by underlying stream.
-
If
-
getUnbufferedReader
Same asgetReader()
, but doesn't encapsulate the result in aBufferedReader
;- Returns:
- An unbuffered reader.
- Throws:
IOException
- Thrown by underlying stream.
-
getInputStream
Returns the HTTP content content as anInputStream
.- Returns:
- The negotiated input stream.
- Throws:
IOException
- If any error occurred while trying to get the input stream or wrap it in the GZIP wrapper.
-
getParserMatch
Returns the parser and media type matching the requestContent-Type header.- Returns:
- The parser matching the request
Content-Type header, orOptional.empty()
if no matching parser was found. Includes the matching media type.
-
getContentLength
Returns the content length of the content.- Returns:
- The content length of the content in bytes.
-
cache
Caches the content in memory for reuse.- Returns:
- This object.
- Throws:
IOException
- If error occurs while reading stream.
-