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

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -