Ali Shakiba
Ali Shakiba

Reputation: 21257

JAX-RS: How to define response type using query parameter instead of Accept header?

In JAX-RS is there any way to define response type using query parameters instead of Accept header (e.g. /api/foo?format=json)?

Upvotes: 1

Views: 628

Answers (3)

tech.yenduri
tech.yenduri

Reputation: 596

In that case you need to enable one switch in web.xml like below

   <context-param>
    <param-name>resteasy.media.type.param.mapping</param-name>
    <param-value>format</param-value>
   </context-param>

/api/foo?format=json

Upvotes: 0

Lan
Lan

Reputation: 6660

There are three ways to do content negotiation in JAX-RS: based on URL, based on Accept header and based on request parameter. Please check out link Content Negotiation Based on Request Parameter. It contains sample code how to do content negotiation on request parameter. The link is for Apache Wink project, but the code should work in Jersey implementation.

Upvotes: 4

Ryan Stewart
Ryan Stewart

Reputation: 128799

I'm not aware of a built-in way to do it based on a query parameter, but the UriConnegFilter supports the suggested method of using a URI suffix, like /api/foo.json. Maybe you could conform to that convention? If not, the UriConnegFilter would be a good starting place for building your own support for using query parameters.

Upvotes: 1

Related Questions