Mapping extension content types in Apache CXF JAX-RS
content-type, cxf, jax-rs, mapping
Solution
Adding the following in your `<jaxrs:server>` works:
<jaxrs:extensionMappings>
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
</jaxrs:extensionMappings>
Source: http://cxf.apache.org/docs/jax-rs.html#JAX-RS-Debugging
Problem
JAX-RS offers a wonderful way to specify content types in `@Produces`, and the framework will automatically determine the best content type from the client's HTTP `Accept` header and, wonder of wonders, even convert your object to that type (e.g. XML using JAXB or JSON using Jackson) when returning information to the caller. My (work) client, as clients often do, made a simple job more difficult by requesting I specify the content type by the extension in the URL, e.g. `api/widgets.json`. This would force me to have various `getWidgetsXXX()` methods, one with `@Produces("application/json")`, another with `@Produces("application/xml")`, etc. But I'm using Apache CXF and I was delighted to find that I could configure CXF to map various extensions to content types using the `jaxrs.extensions` init parameter! ``` <!-- registers extension mappings --> <init-param> <param-name>jaxrs.extensions</param-name> <param-value> xml=application/xml json=application/json </param-value> </init-param> ``` But I can find absolutely no documentation on how this works in the real world. I naively thought I could just annotate a method with a path with an extension and it would mimic the `Accepts` header: ``` @Path("/widgets.{extension}") @GET @Produces({ "application/json", "application/xml" }) public List<Widget> getWidgets(); ``` So I call it using `api/widgets.json`, and it returns XML! Which is particularly odd, because JAX-RS specifies that the default content type is the first one listed. Where can I find out how to use CXF extension content type mapping? P.S. I am not using Spring.