Stream Support

Micronaut also supports binding the body to an InputStream. If the method is reading the stream, the method execution must be offloaded to another thread pool to avoid blocking the event loop.

Performing Blocking I/O With InputStream

  1. @Post(value = "/read", processes = MediaType.TEXT_PLAIN)
  2. @ExecuteOn(TaskExecutors.IO) (1)
  3. String read(@Body InputStream inputStream) throws IOException { (2)
  4. return IOUtils.readText(new BufferedReader(new InputStreamReader(inputStream))); (3)
  5. }

Performing Blocking I/O With InputStream

  1. @Post(value = "/read", processes = MediaType.TEXT_PLAIN)
  2. @ExecuteOn(TaskExecutors.IO) (1)
  3. String read(@Body InputStream inputStream) throws IOException { (2)
  4. IOUtils.readText(new BufferedReader(new InputStreamReader(inputStream))) (3)
  5. }

Performing Blocking I/O With InputStream

  1. @Post(value = "/read", processes = [MediaType.TEXT_PLAIN])
  2. @ExecuteOn(TaskExecutors.IO) (1)
  3. fun read(@Body inputStream: InputStream): String { (2)
  4. return IOUtils.readText(BufferedReader(InputStreamReader(inputStream))) (3)
  5. }
1The controller method is executed on the IO thread pool
2The body is passed to the method as an input stream
3The stream is read