Jersey 1.8中Response.header是否覆盖@Produces注解?响应Content-Type以何为准?
Jersey 1.8: @Produces vs Response.header() for Content-Type
Hey there, let's cut straight to the answer: your response will use Content-Type: application/json;charset=utf-8 — the value you explicitly set via Response.header().
Here's the breakdown of why this happens in Jersey 1.8:
- The
@Producesannotation is primarily for request routing and matching. It tells Jersey what media types your resource method is capable of generating, so the framework can match it to incoming requests based on the client'sAcceptheader. It doesn't enforce a hard lock on the final response's Content-Type. - When you manually set the
Content-Typeheader usingResponse.header(), this explicit value takes priority. Jersey will honor this override over the@Producesannotation's value when constructing the final HTTP response.
If you want to verify this yourself, grab a tool like curl or check your browser's developer tools:
curl -i http://your-api-base-url/test
You'll see the Content-Type header in the response matches the JSON type you set in code, not the TEXT_HTML from the annotation.
内容的提问来源于stack exchange,提问作者bhattedon
相关产品推荐
相关产品推荐

