在 RestEasy 中从 ExceptionMapper 返回 JSON 的简单方法?
情况是这样的:
- 我让 RESTEasy 和 Jackson 完美地协同工作。
- 我的 RESTful 类实现了 ExceptionMapper,并且有一个 toResponse 方法。
- 如果我的任何 RESTful 方法中出现问题,则会抛出异常并由 toResponse 方法捕获。
- 我希望将异常转换为格式良好的 XML 或 JSON(取决于 Accept 标头)
问题是我不知道 toResponse 方法中的 Accept 标头是什么。我知道我可以扩展 Exception 类并将标头作为变量传递,然后我可以在 toResponse 中使用它,但我希望有一种更“正确”的方法来做到这一点。我尝试将 @Produces 注释添加到 toResponse 方法,但没有成功。
有人成功配置 RESTEasy 以轻松从 toResponse 返回 JSON 或 XML 吗?
-担
Here's the situation:
- I have RESTEasy and Jackson working together beautifully
- My RESTful classes implement ExceptionMapper and have a toResponse method on them.
- If I have a problem inside any of my RESTful methods an Exception is thrown and caught by the toResponse method.
- I'd like the exception to turn into nicely formatted XML or JSON (depending on the Accept header)
The problem is I have no idea what the Accept header is inside the toResponse method. I know I can extend the Exception class and pass the header in as a variable, which I can then use in toResponse, but I was hoping there would be a more 'correct' way to do this. I've tried adding the @Produces annotation to the toResponse method without success.
Has anyone successfully configured RESTEasy to easily return JSON or XML from toResponse?
-Dan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没关系,我找到了答案。我的问题措辞一定很糟糕,但我会留下这个,以防有人偶然发现它。
答案如下:
带有 XML 或 JSON 的 JAX-RS (Jersey) 自定义异常
Nevermind, I found the answer. I must have worded my question poorly, but I will leave this around in case someone stumbles upon it.
Here's the answer:
JAX-RS (Jersey) custom exception with XML or JSON