使用ObjectMapper添加JAR使我的ObjectMapper不可发现 [英] Adding JAR with ObjectMapper makes my ObjectMapper non-discoverable

查看:376
本文介绍了使用ObjectMapper添加JAR使我的ObjectMapper不可发现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在jar中从依赖项中定义了另一个对象映射器,如何使我的对象映射器工作?

How can I make my object mapper work in situation when there is another object mapper defined in jar from dependencies ?

我正在尝试使用 Jersey 2 http://swagger.io/ =noreferrer> Swagger 这是在Jetty下运行的。问题是,只要我将Swagger JAX-RX jar添加到类路径中,就不会发现我的对象映射器,因此我丢失了对象的自定义序列化。

I'm trying to use Swagger with Jersey 2 which is being run under Jetty. The problem is that as soon as I add Swagger JAX-RX jar into classpath my object mapper is not discovered therefore I lose custom serialization of my objects.

这是我的定义对象映射器

Here is how my object mapper defined

@Provider
public class ObjectMapperProvider implements ContextResolver<ObjectMapper> {
}

我发布了问题给Swagger的维护人员,你可以在那里阅读详细信息。

I've posted issue to Swagger's maintainers where you could read details.

之后在Jersey的内部调试我发现Swagger自己的对象映射器 com.wordnik.swagger.jaxrs.json.JacksonJsonProvider 调用 super.setMapper(commonMapper)将非空值设置为 ProviderBase._mapperConfig._mapper 。稍后当http请求处理程序尝试序列化我的类调用的实例时,最终会出现在 ProviderBase.locateMapper 中,其中包含以下正文

After hours of debugging in internals of Jersey I found that Swagger's own object mapper com.wordnik.swagger.jaxrs.json.JacksonJsonProvider calls super.setMapper(commonMapper) that sets non-null value to ProviderBase._mapperConfig._mapper. Later when http request handler attempts to serialize instance of my class call ends up in ProviderBase.locateMapper which has following body

public MAPPER locateMapper(Class<?> type, MediaType mediaType)
{
    // First: were we configured with a specific instance?
    MAPPER m = _mapperConfig.getConfiguredMapper();
    if (m == null) {
        // If not, maybe we can get one configured via context?
        m = _locateMapperViaProvider(type, mediaType);
        if (m == null) {
            // If not, let's get the fallback default instance
            m = _mapperConfig.getDefaultMapper();
        }
    }
    return m;
}

正确的代码流 _mapperConfig.getConfiguredMapper() 返回null,随后导致调用 _locateMapperViaProvider 找到我的自定义映射器。使用Swagger,它默认为 com.fasterxml.jackson.jaxrs.json.JsonMapperConfigurator ,我的自定义json序列化器永远不会被调用。

in correct code-flow _mapperConfig.getConfiguredMapper() returns null which subsequently causes invocation of _locateMapperViaProvider which finds my custom mapper. With Swagger it defaults to com.fasterxml.jackson.jaxrs.json.JsonMapperConfigurator and my custom json serializers never get invoked.

我创建了一个小项目来重现这个问题这里

I created small project which reproduces this problem here.

你们有什么建议解决这个问题?我可以在 TTLocalDate 类型的每个属性上指定反序列化器,但它会污染代码:(

How would you guys suggest to fix this ? I could probably specify deserializer on each property of type TTLocalDate but it'll pollute the code :(

推荐答案

正如fehguy在问题报告中指出的那样,使用最新的Swagger版本并使用 SwaggerSerializers 应解决此问题。之前的Swagger JacksonJsonProvider 将用于所有序列号, SwaggerSerializers 仅限用于 Swagger 模型对象

As noted by fehguy in the issue report, using the latest Swagger release and using the SwaggerSerializers should fix this issue. Where previously the Swagger JacksonJsonProvider would be used for all serializions, the SwaggerSerializers is only used for the Swagger model object

public class SwaggerSerializers implements MessageBodyWriter<Swagger> {

    @Override
    public boolean isWriteable(Class type, Type genericType, Annotation[] annotations,
                               MediaType mediaType) {
        return Swagger.class.isAssignableFrom(type);
    }

这篇关于使用ObjectMapper添加JAR使我的ObjectMapper不可发现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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