Content Negotiation Module
The content negotiation module helps using custom media types in your Play! application. For this it uses annotations to express which media type a controller method can produce.
Enable the content negotiation module
For applications running Play! < 1.2 change the /conf/application.conf file and enable the content negotiation module by adding this line:
# The content negotiation module
module.cnm=${play.path}/modules/cnm-2.0
For applications running Play! >= 1.2 change the /conf/dependencies.yml file and enable the content negotiation module by adding this line:
- play -> cnm 2.0
Usage
Annotate all controller methods which may return a custom media type not supported by the Play! framework directly with the Supports
annotation. Inside the Supports
annotation list all supported media types by using the MediaType
annotation.
Example
Suppose you have a method like the following:
public static void index() {
render();
}
And you wish to support the application/vnd.com.example.play+xml media type by mapping it to the .play template format. To do so simply add the following line to the method:
@Supports(@MediaType(accept="application/vnd.com.example.play+xml", format="play"))
public static void index() {
render();
}
Multiple types are supported as well:
@Supports(@MediaType(accept="application/vnd.com.example.play+xml", format="play"),
@MediaType(accept="text/x-vcard", format="vcard"))
public static void index() {
render();
}
Request a specific content type
Besides defining your own content type and the corresponding template format, you may want at some point to actually use it. To do so change the Accept header of your HTTP request to be exactly like the content type you want. Multiple entries, each with different q-values are supported as well.
Accept: application/vnd.com.example.play+xml
Accept: application/vnd.com.example.play+xml;q=1.0, application/vnd.com.example.play+json;q=0.9
Issue tracking
If you wish to report a bug or wish for a new feature, please use the issue tracker