本文介绍了定制杰克逊序列化的resteasy客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以为resteasy客户端注册自定义Jackson JSON序列化程序?

Is it possible to register a custom Jackson JSON serializer for the resteasy client?

我尝试过这样的事情:

ResteasyClient client = new ResteasyClientBuilder()
            .register(new CustomSerializer())
            .build();

但它不起作用。任何想法?

But it didn't work. Any ideas?

推荐答案

您只能注册JAX-RS组件,如或。
你需要注册一个带有ObjectMapper的Provider,它带有一个模块,你的序列化器已注册。

You can only register JAX-RS components like Providers or Features.You need to register an Provider which carries an ObjectMapper which carries an Module with your serializer registered.

ResteasyJacksonProvider resteasyJacksonProvider = new ResteasyJacksonProvider();
ObjectMapper mapper = new ObjectMapper();
SimpleModule myModule = new SimpleModule("myModule", new Version(1, 0, 0, null));
myModule.addSerializer(Custom.class, new CustomSerializer());
mapper.registerModule(myModule);
resteasyJacksonProvider.setMapper(mapper);
ResteasyClient client = new ResteasyClientBuilder().register(resteasyJacksonProvider).build();

请注意,此示例使用 org.codehaus.jackson 。来自 com.fasterxml.jackson 的API看起来略有不同。

Note that this example is using the classes from org.codehaus.jackson. The API from com.fasterxml.jackson looks slightly different.

这篇关于定制杰克逊序列化的resteasy客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 07:00