如何从JAX-RS客户端提取ObjectMapper? [英] How to extract ObjectMapper from JAX-RS Client?

查看:134
本文介绍了如何从JAX-RS客户端提取ObjectMapper?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jersey JAX-RS客户端(版本2.0)。我知道它使用Jackson ObjectMapper来生成和解析JSON。我想使用同一个对象为某些java类生成JSON,以便我可以将它们写入日志。

I am using Jersey JAX-RS client (version 2.0). I know it is using a Jackson ObjectMapper to generate and parse JSON. I want to use that same object to generate JSON for some java classes so that I can write them to a log.

我知道我可以创建一个新的ObjectMapper实例但是我更愿意请泽西岛客户给我一个它正在使用的那个。我怎样才能做到这一点? Jersey 2.0知道Jackson,因为它包含一个JacksonFeature类,用于首先配置Jackson功能。

I know I can create a new instance of ObjectMapper but I prefer to request Jersey Client to give me a reference to the one it is using. How can I do this? Jersey 2.0 is aware of Jackson because it includes a JacksonFeature class that is used to configure the Jackson feature in the first place.

推荐答案

我通过添加以下静态成员解决了这个问题:

I solved this by adding the following static members:

private static JacksonJsonProvider jackson_json_provider = new JacksonJaxbJsonProvider()
      .configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false)
      .configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);

private static ObjectMapper object_mapper = jackson_json_provider.locateMapper(
      Object.class, MediaType.APPLICATION_JSON_TYPE);

private static Client client = ClientBuilder.newClient().register(jackson_json_provider);

请注意,仅配置 FAIL_ON_UNKNOWN_PROPERTIES FAIL_ON_EMPTY_BEANS ;出于其他原因,我使用 object_mapper

Note that the second declaration is not needed just to configure FAIL_ON_UNKNOWN_PROPERTIES or FAIL_ON_EMPTY_BEANS; I use object_mapper for some other reasons.

这篇关于如何从JAX-RS客户端提取ObjectMapper?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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