在泽西岛使用 Gson 而不是 Jackson [英] Using Gson instead of Jackson in Jersey

查看:28
本文介绍了在泽西岛使用 Gson 而不是 Jackson的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用和生成 JSON 对象的 RESTful 服务,我希望 Jersey 使用 Gson 而不是 Jackson.

I have a RESTful service that consumes and produces JSON objects, and I would like Jersey to use Gson instead of Jackson.

这怎么能……?

推荐答案

你需要编写MessageBodyReaderMessageBodyWriter(可能在同一个类中)并注册到 Jersey(如果你使用包扫描,@Provider 注释就足够了)——很像JacksonJsonProvider 做到了:

You need to write custom implementations of MessageBodyReader and MessageBodyWriter (possibly in the same class) and register with Jersey (if you use package scanning, the @Provider annotation is enough) -- pretty much like JacksonJsonProvider does it:

@Provider
@Consumes({MediaType.APPLICATION_JSON, "text/json"})
@Produces({MediaType.APPLICATION_JSON, "text/json"})
class GsonJsonProvider implements
    MessageBodyReader<Object>,
    MessageBodyWriter<Object> { ...

这篇关于在泽西岛使用 Gson 而不是 Jackson的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆