java - How to get the requested contenttype of a REST-Request? -
using jaxb implementing rest-webservice, have several methods producing output.
the class contains of these methods annotated @produces({ mediatype.application_xml, mediatype.application_json })
. if request goes happy-path (no errors occur), return pojo's in our methods , jaxb dynamically marshalls these objects application/xml
or application/json
, client requested via accept: application/xxx;
in request header.
my question how requested content type, because if error occurs, throwing webapplicationexception
response should contain custom error message formatted requested content type.
you could...
inject @headerparam("accept")
public response dosomething(@headerparam("accept") string accept) { // may need parse value not // simple application/json }
you could...
inject httpheaders
, have couple options
public response dosomething(@context httpheaders headers) { string accept = headers.getheaderstring(httpheaders.accept); list<mediatype> acceptabletype = headers.getacceptablemediatypes(); }
Comments
Post a Comment