简体   繁体   中英

JAX-RS: retrieve alternate format based on URL pattern

I have a simple JAX-RS service running in a Tomcat 6 container. Is it possible to display not only text but XML in the browser, in a comparable manner as in Rails by appending .xml or .json ?

The service I have is based on this tutorial .

Short answer is no , JAX-RS does not allow you to switch the response content type simply by appending 'xml' or 'json' to the URL.

The specification explicitly defines how content negotiation happens - via the Accept HTTP header from the client side, matching with the @Produces annotation on the server side. So going by the spec, if you want to get a different content type then you would specify as such in an Accept header, and that content type would be returned (as long as the server supports it).

Having said that, JAX-RS implementations are free to implement custom (non-portable) extensions to support behavior similar to what you describe. An example would be RESTEasy, which allows you to define the desired content type as a query parameter Section 17.2 - Query String Parameter-based negotiation .

You could also design your resource classes to easily support the desired behavior - some examples can be seen in the Apache CXF Content Negotiation guide .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM