JAX-RS Jersey - 如何强制响应内容类型?覆盖协商内容
Jersey 通过查看接受标头来识别请求。我有一个仅接受 text/* 的请求 - 我如何强制响应为例如 application/json?
@POST
@Path("/create")
@Produces(MediaType.APPLICATION_JSON)
public MyResponseObject create() {
return new MyResponseObject();
}
如果请求定向到仅接受 text/* jersey 的 create 将返回 500。有没有办法解决此问题? (我无法更改请求接受标头)。
Jersey identifies requests by looking at the accept header. I have a request which accepts only text/* - How can i force the response to be for example application/json?
@POST
@Path("/create")
@Produces(MediaType.APPLICATION_JSON)
public MyResponseObject create() {
return new MyResponseObject();
}
If a request is directed to create which only accepts text/* jersey will return a 500. Is there a way to workaround this issue? (I can't change the requests accept header).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Jersey 还通过 支持此功能您可以在 web.xml 中配置或通过 Jersey API 以编程方式配置 ResourceConfig 属性 PROPERTY_MEDIA_TYPE_MAPPINGS,如下所示:
您可以通过在 URL 中添加 .json 或 .xml 后缀来强制进行内容类型协商。
Jersey also supports this via ResourceConfig property PROPERTY_MEDIA_TYPE_MAPPINGS that you could configure in your web.xml or programatically via Jersey APIs as shown below:
You can force content type negotiation by suffixing either .json or .xml to your URL.
我通过使用 servlet 过滤器解决了这个问题:
http://www.zienit.nl/blog/2010/01/rest/control-jax-rs-content-negotiation-with-filters
I solved this by using a servlet filter:
http://www.zienit.nl/blog/2010/01/rest/control-jax-rs-content-negotiation-with-filters