Jersey 2.9和Jackson 1。x为JSON提供者

时间:2021-02-24 19:33:26

I have a dependency to the third-party library Woorea Openstack-SDK (https://github.com/woorea/openstack-java-sdk) which uses Jackson 1.x annotations. Because of the Jackson update (Jackson 1.x -> Jackson 2.x) in Jersey 2.9, the Openstack-SDK becomes incompatible.

我依赖于使用Jackson 1的第三方库Woorea Openstack-SDK (https://github.com/woorea/openstack-java-sdk)。x注释。因为杰克逊的更新(杰克逊1)。在Jersey 2.9中,Openstack-SDK变得不兼容。

Is there a way to use Jersey 2.9 together with Jackson 1.x as JSON provider?

是否有办法使用Jersey 2.9和Jackson 1一起使用。x为JSON提供者?

2 个解决方案

#1


0  

I used https://github.com/FasterXML/jackson-jaxrs-providers/ to provide Jackson 2.x to previous versions of Jersey (wihtout Jackson 2.x support). Should be an alternative to provide now Jackson 1.x to new versions of Jersey(with Jackson 2.0 support). Otherwise, check the implementation in the above link. You could do the same, since it's mostly about registering a new provider.

我使用https://github.com/FasterXML/jackson-jaxrs-providers/来提供Jackson 2。x到之前的球衣(杰克逊2号)。x支持)。应该成为杰克逊1号的替代者。x到新版本的Jersey(使用Jackson 2.0支持)。否则,请检查上面链接中的实现。您也可以这样做,因为它主要是关于注册一个新的提供者。

#2


0  

I found a solution... I removed the dependency to the artifact jersey-media-json-jackson and registered the following feature:

我找到了一个解决办法……我删除了对神器jersey-media-json-jackson的依赖,并注册了以下特性:

package test;

import javax.ws.rs.core.Configuration;
import javax.ws.rs.core.Feature;
import javax.ws.rs.core.FeatureContext;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.MessageBodyWriter;

import org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider;
import org.codehaus.jackson.jaxrs.JsonMappingExceptionMapper;
import org.codehaus.jackson.jaxrs.JsonParseExceptionMapper;
import org.glassfish.jersey.CommonProperties;
import org.glassfish.jersey.internal.InternalProperties;
import org.glassfish.jersey.internal.util.PropertiesHelper;

public class Jackson1xFeature implements Feature {

private final static String JSON_FEATURE = Jackson1xFeature.class.getSimpleName();

@Override
public boolean configure(final FeatureContext context) {
    final Configuration config = context.getConfiguration();

    final String jsonFeature = CommonProperties.getValue(config.getProperties(), config.getRuntimeType(), InternalProperties.JSON_FEATURE, JSON_FEATURE, String.class);
    // Other JSON providers registered.
    if (!JSON_FEATURE.equalsIgnoreCase(jsonFeature)) {
        return false;
    }

    // Disable other JSON providers.
    context.property(PropertiesHelper.getPropertyNameForRuntime(InternalProperties.JSON_FEATURE, config.getRuntimeType()), JSON_FEATURE);

    // Register Jackson.
    if (!config.isRegistered(JacksonJaxbJsonProvider.class)) {
        // add the default Jackson exception mappers
        context.register(JsonParseExceptionMapper.class);
        context.register(JsonMappingExceptionMapper.class);
        context.register(JacksonJaxbJsonProvider.class, MessageBodyReader.class, MessageBodyWriter.class);
    }

    return true;
}

}

#1


0  

I used https://github.com/FasterXML/jackson-jaxrs-providers/ to provide Jackson 2.x to previous versions of Jersey (wihtout Jackson 2.x support). Should be an alternative to provide now Jackson 1.x to new versions of Jersey(with Jackson 2.0 support). Otherwise, check the implementation in the above link. You could do the same, since it's mostly about registering a new provider.

我使用https://github.com/FasterXML/jackson-jaxrs-providers/来提供Jackson 2。x到之前的球衣(杰克逊2号)。x支持)。应该成为杰克逊1号的替代者。x到新版本的Jersey(使用Jackson 2.0支持)。否则,请检查上面链接中的实现。您也可以这样做,因为它主要是关于注册一个新的提供者。

#2


0  

I found a solution... I removed the dependency to the artifact jersey-media-json-jackson and registered the following feature:

我找到了一个解决办法……我删除了对神器jersey-media-json-jackson的依赖,并注册了以下特性:

package test;

import javax.ws.rs.core.Configuration;
import javax.ws.rs.core.Feature;
import javax.ws.rs.core.FeatureContext;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.MessageBodyWriter;

import org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider;
import org.codehaus.jackson.jaxrs.JsonMappingExceptionMapper;
import org.codehaus.jackson.jaxrs.JsonParseExceptionMapper;
import org.glassfish.jersey.CommonProperties;
import org.glassfish.jersey.internal.InternalProperties;
import org.glassfish.jersey.internal.util.PropertiesHelper;

public class Jackson1xFeature implements Feature {

private final static String JSON_FEATURE = Jackson1xFeature.class.getSimpleName();

@Override
public boolean configure(final FeatureContext context) {
    final Configuration config = context.getConfiguration();

    final String jsonFeature = CommonProperties.getValue(config.getProperties(), config.getRuntimeType(), InternalProperties.JSON_FEATURE, JSON_FEATURE, String.class);
    // Other JSON providers registered.
    if (!JSON_FEATURE.equalsIgnoreCase(jsonFeature)) {
        return false;
    }

    // Disable other JSON providers.
    context.property(PropertiesHelper.getPropertyNameForRuntime(InternalProperties.JSON_FEATURE, config.getRuntimeType()), JSON_FEATURE);

    // Register Jackson.
    if (!config.isRegistered(JacksonJaxbJsonProvider.class)) {
        // add the default Jackson exception mappers
        context.register(JsonParseExceptionMapper.class);
        context.register(JsonMappingExceptionMapper.class);
        context.register(JacksonJaxbJsonProvider.class, MessageBodyReader.class, MessageBodyWriter.class);
    }

    return true;
}

}