Skip to content

Instantly share code, notes, and snippets.

@danbev
Last active December 9, 2015 23:29
Show Gist options
  • Select an option

  • Save danbev/4344933 to your computer and use it in GitHub Desktop.

Select an option

Save danbev/4344933 to your computer and use it in GitHub Desktop.
Accept '*/* media type

In aerogear-controller we have support for specifying what media type that a route produces:

route()
    .from("/car/{id}")
    .on(RequestMethod.GET)
    .produces(MediaType.HTML, MediaType.JSON)
    .to(SampleController.class).find(param("id"));
}

In this case we can see that it produces 'text/html' and 'application/json' which are the values of the MediaType enums here. So, when a client makes a request they can specify that the media type they wish to have returned by using the HTTP Accept Header. If the produces method is not specified it defaults to MediaType.HTML. If the Accept header is text/html then the request will be forwarded to a view, and if the Accept header is application/json then JSON will be returned.

We need to match a request to a route and there is the possibility that two or more routes have the same 'from' path, but produce different media types.

route()
    .from("/car/{id}")
    .on(RequestMethod.GET)
    .to(SampleController.class).find(param("id"));
}
route()
    .from("/car/{id}")
    .on(RequestMethod.GET)
    .produces("application/custom")
    .to(SampleController.class).find(param("id"));
}

So there are two places where we need to consider the media type a route produces:

  1. For matching request to a route
  2. For matching a Responder to a media type (which handles the response, either forwarding to a view or returning the appropiate media type)

Now, there is also the usecase where the request contains 'Accept: /' which means it does not care, just give me whatever the route is configured to return.

  • how about we return the Responder for the first media type in the produces list of the route?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment