Routing based on MIME type of request

你可以通过consumes方法指定route需要匹配请求的MIME类型.

这下面的例子中, 请求包含了一个content-type请求头,该值指定了请求体的MINE类型. 这个值会和consumes方法里的值进行匹配.

MINE类型的匹配可以做到精确匹配.

  1. router.route().consumes("text/html").handler(routingContext -> {
  2. // This handler will be called for any request with
  3. // content-type header set to `text/html`
  4. });

同样我们还可以进行多个MINE类型的匹配

  1. router.route().consumes("text/html").consumes("text/plain").handler(routingContext -> {
  2. // This handler will be called for any request with
  3. // content-type header set to `text/html` or `text/plain`.
  4. });

我们还可以通过通配符对子类型进行匹配

  1. router.route().consumes("text/*").handler(routingContext -> {
  2. // This handler will be called for any request with top level type `text`
  3. // e.g. content-type header set to `text/html` or `text/plain` will both match
  4. });

我们还可以通过通配符对父类型进行匹配

  1. router.route().consumes("*/json").handler(routingContext -> {
  2. // This handler will be called for any request with sub-type json
  3. // e.g. content-type header set to `text/json` or `application/json` will both match
  4. });

如果你在consumers不指定/, 它会假定你指的是子类型.