Class RequestContent

java.lang.Object
org.apache.juneau.rest.httppart.RequestContent

public class RequestContent extends Object
Contains the content of the HTTP request.

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:

@RestPost(...) public Object myMethod(RequestContent content) {...}

Example:

@RestPost(...) public void doPost(RequestContent content) { // Convert content to a linked list of Person objects. List<Person> list = content.as(LinkedList.class, Person.class); ... }

Some important methods on this class are:

See Also:
  • Constructor Details

  • 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

      public RequestContent maxInput(long value)
      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

      public RequestContent parser(Parser value)
      Sets the parser to use for this content.
      Parameters:
      value - The new value for this setting.
      Returns:
      This object.
    • content

      public RequestContent content(byte[] value)
      Sets the contents of this content.
      Parameters:
      value - The new value for this setting.
      Returns:
      This object.
    • as

      public <T> T as(Class<T> type) throws BadRequest, UnsupportedMediaType, InternalServerError
      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 or Object.class, then the actual type will be determined automatically based on the following input:

      TypeJSON inputXML inputReturn 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 <null/> or blank
      <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. MyBean content3 = req.getContent().as(MyBean.class); // Parse into a linked-list of objects. List content4 = req.getContent().as(LinkedList.class); // Parse into a map of object keys/values. Map content5 = 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 an IOException occurs.
    • 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 an IOException occurs.
    • asString

      public String asString() throws IOException
      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.
    • asBytes

      public byte[] asBytes() throws IOException
      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.
    • asHex

      public String asHex() throws IOException
      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

      public String asSpacedHex() throws IOException
      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 a Reader.
      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.
    • getUnbufferedReader

      Same as getReader(), but doesn't encapsulate the result in a BufferedReader;
      Returns:
      An unbuffered reader.
      Throws:
      IOException - Thrown by underlying stream.
    • getInputStream

      public jakarta.servlet.ServletInputStream getInputStream() throws IOException
      Returns the HTTP content content as an InputStream.
      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 request Content-Type header.
      Returns:
      The parser matching the request Content-Type header, or Optional.empty() if no matching parser was found. Includes the matching media type.
    • getContentLength

      public int getContentLength()
      Returns the content length of the content.
      Returns:
      The content length of the content in bytes.
    • cache

      public RequestContent cache() throws IOException
      Caches the content in memory for reuse.
      Returns:
      This object.
      Throws:
      IOException - If error occurs while reading stream.