在 RestEasy 中访问 Jackson 对象映射器
我被要求美化来自 RestEasy 端点的默认 Jackson JSON。我对 Jackson 做了一些研究,并编写了一些独立代码,以便能够抑制空值、自定义数据格式等。现在的挑战是将这些代码注入 RestEasy 的 JSON 序列化中。
从论坛帖子来看,这在 Spring 中是微不足道的,但在 RestEasy 中似乎并非如此。我编写了一个 ContextResolver 并在 web.xml(在 Tomcat 上)的上下文参数中配置为resteasy.provider,但这会阻止 web 应用程序在 Tomcat 上加载。
现在我尝试扩展 javax.ws.rs.core.Application 并提供 ContextResolver 但没有取得任何进展。这个简单吗,有人做过吗?非常感谢任何帮助。
I've been asked to beautify default Jackson JSON coming out of a RestEasy endpoint. I did some research on Jackson and wrote some standalone code to be able to suppress nulls, customize data formats etc. Now the challenge is injecting this code in RestEasy's JSON serialization.
Judging from the forum posts this is trivial in Spring, however doesn't seem to be the case in RestEasy. I wrote a ContextResolver and configured as resteasy.provider in context params in web.xml (on Tomcat) but that prevents the webapp from loading on Tomcat.
Now I'm trying to extend javax.ws.rs.core.Application and provide a ContextResolver but making no progress. Is this straight forward, has anyone done this? Any help is greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我找到了一种更好的修改 Jackson SerializationConfig 的方法 - 您可以使用 JAX-RS ContextResolver 拦截 ObjectMapper 创建。
您需要通过以下方式之一向 RESTEasy 注册:
参考:RESTEasy 文档
参考:JBoss 论坛上的 Nicklas Karlsson
请注意,这适用于作为模块提供的 RESTEasy 2.3.2在 JBoss 7.1.1.Final 中,但似乎不适用于 RESTEasy 3.0-beta5。
I found a nicer way of modifying the Jackson SerializationConfig - you can intercept the ObjectMapper creation by using a JAX-RS ContextResolver.
You will need to register with RESTEasy in one of the following ways:
Reference: RESTEasy docs
Reference: Nicklas Karlsson on the JBoss forums
Please note that this works with RESTEasy 2.3.2 which ships as a module in JBoss 7.1.1.Final, but does not appear to work with RESTEasy 3.0-beta5.
好吧,我想通了,我可以通过基于 Jackson FAQ:JAX-RS 编写自定义 JacksonJsonProvider 来做到这一点。代码如下:
Ok,I figured it out, I was able to do this by writing a custom JacksonJsonProvider based on the Jackson FAQ: JAX-RS.The code is as follows:
如果您使用的是 Jackson2 提供程序,您需要做一些与之前的答案略有不同的事情。默认情况下,此示例将漂亮地打印输出
并将其注册到您的 web-xml 中,如果您没有启用自动注册,请将其添加到您的resteasy.providers context-param
If you're using the Jackson2 provider you need to do something slightly different from the previous answer. This example will pretty-print the output by default
and to register it in your web-xml, if you don't have autoregister on, add it to your resteasy.providers context-param
我做了很多工作来在我的应用程序中注册它,即使在我使用 spring boot 并且没有 web.xml 时遵循使用 context-param 的提示,这里我做了什么
自定义提供程序
在应用程序
META-INF/services/ 上注册javax.ws.rs.ext.Providers
I had a lot of work to register it in my application even following the tip to use context-param once I'm using spring boot and have not web.xml, here what I did
Custom provider
Registering on application
META-INF/services/javax.ws.rs.ext.Providers
Jackson
ObjectMapper
的提供程序应该是执行此操作的标准 JAX-RS 方式(与 Jersey 一起使用),因此它似乎也是使用 RESTeasy 的方式。Provider for Jackson
ObjectMapper
should be standard JAX-RS way of doing this (works with Jersey), so it seems like the way to go with RESTeasy as well.© RESTEasy 用户指南 。
这不是一个全局解决方案,但您也可以将注释放在类上。
© RESTEasy User Guide.
This is not a global solution, but you can put the annotation on classes too.