The following example implements a simple echo server that echos the body sent in the request:

    Using the @Body annotation

    1. import io.micronaut.http.MutableHttpResponse
    2. import io.micronaut.http.annotation.Body
    3. import io.micronaut.http.annotation.Controller
    4. import io.micronaut.http.annotation.Post
    5. import io.reactivex.Flowable
    6. import io.reactivex.Single
    7. @Post(value = "/echo", consumes = MediaType.TEXT_PLAIN) (1)
    8. String echo(@Size(max = 1024) @Body String text) { (2)
    9. }

    Using the @Body annotation

    Note that reading the request body is done in a non-blocking manner in that the request contents are read as the data becomes available and accumulated into the String passed to the method.

    Using RxJava 2 to Read the request body

    1. @Post(value = "/echo-flow", consumes = MediaType.TEXT_PLAIN) (1)
    2. return text.collect(StringBuffer::new, StringBuffer::append) (3)
    3. HttpResponse.ok(buffer.toString())
    4. );
    5. }

    Using RxJava 2 to Read the request body

    1. @Post(value = "/echo-flow", consumes = [MediaType.TEXT_PLAIN]) (1)
    2. open fun echoFlow(@Body text: Flowable<String>): Single<MutableHttpResponse<String>> { (2)
    3. return text.collect({ StringBuffer() }, { obj, str -> obj.append(str) }) (3)
    4. .map { buffer -> HttpResponse.ok(buffer.toString()) }
    5. }