6.10 Accepted Request Content-Type

A Micronaut’s controller action consumes application/json by default. Consuming other content types is supported with the @Consumes annotation, or the consumes member of any HTTP method annotation.

  1. import io.micronaut.context.annotation.Requires;
  2. import io.micronaut.http.HttpResponse;
  3. import io.micronaut.http.MediaType;
  4. import io.micronaut.http.annotation.Consumes;
  5. import io.micronaut.http.annotation.Controller;
  6. import io.micronaut.http.annotation.Post;
  7. @Controller("/consumes")
  8. public class ConsumesController {
  9. @Post (1)
  10. public HttpResponse index() {
  11. return HttpResponse.ok();
  12. }
  13. @Consumes({MediaType.APPLICATION_FORM_URLENCODED, MediaType.APPLICATION_JSON}) (2)
  14. @Post("/multiple")
  15. public HttpResponse multipleConsumes() {
  16. return HttpResponse.ok();
  17. }
  18. @Post(value = "/member", consumes = MediaType.TEXT_PLAIN) (3)
  19. public HttpResponse consumesMember() {
  20. return HttpResponse.ok();
  21. }
  22. }
  1. import io.micronaut.context.annotation.Requires
  2. import io.micronaut.http.HttpResponse
  3. import io.micronaut.http.MediaType
  4. import io.micronaut.http.annotation.Consumes
  5. import io.micronaut.http.annotation.Controller
  6. import io.micronaut.http.annotation.Post
  7. @Controller("/consumes")
  8. class ConsumesController {
  9. @Post (1)
  10. HttpResponse index() {
  11. HttpResponse.ok()
  12. }
  13. @Consumes([MediaType.APPLICATION_FORM_URLENCODED, MediaType.APPLICATION_JSON]) (2)
  14. @Post("/multiple")
  15. HttpResponse multipleConsumes() {
  16. HttpResponse.ok()
  17. }
  18. @Post(value = "/member", consumes = MediaType.TEXT_PLAIN) (3)
  19. HttpResponse consumesMember() {
  20. HttpResponse.ok()
  21. }
  22. }
  1. import io.micronaut.context.annotation.Requires
  2. import io.micronaut.http.HttpResponse
  3. import io.micronaut.http.MediaType
  4. import io.micronaut.http.annotation.Consumes
  5. import io.micronaut.http.annotation.Controller
  6. import io.micronaut.http.annotation.Post
  7. @Controller("/consumes")
  8. class ConsumesController {
  9. @Post (1)
  10. fun index(): HttpResponse<*> {
  11. return HttpResponse.ok<Any>()
  12. }
  13. @Consumes(MediaType.APPLICATION_FORM_URLENCODED, MediaType.APPLICATION_JSON) (2)
  14. @Post("/multiple")
  15. fun multipleConsumes(): HttpResponse<*> {
  16. return HttpResponse.ok<Any>()
  17. }
  18. @Post(value = "/member", consumes = [MediaType.TEXT_PLAIN]) (3)
  19. fun consumesMember(): HttpResponse<*> {
  20. return HttpResponse.ok<Any>()
  21. }
  22. }
1By default, a controller’s action consumes request with Content-Type of type application/json.
2@Consumes annotation takes a String[] of supported media types for an incoming request.
3The consumes can also be specified with the consumes member of the method annotation.

Customizing Processed Content Types

Normally the JSON parsing only happens if the content type is application/json. The other MediaTypeCodec classes behave in a similar manner that they have pre-defined content types they can process. To extend the list of media types that a given codec should process, you can provide configuration that will be stored in CodecConfiguration:

  1. micronaut:
  2. codec:
  3. json:
  4. additionalTypes:
  5. - text/javascript
  6. - ...

Currently supported configuration prefixes are json, json-stream, text, and text-stream.