All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.bdeploy.jersey.JerseyObjectMapper Maven / Gradle / Ivy

Go to download

Public API including dependencies, ready to be used for integrations and plugins.

There is a newer version: 7.3.6
Show newest version
package io.bdeploy.jersey;

import java.util.Collections;

import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper;

import io.bdeploy.common.util.JacksonHelper;
import io.bdeploy.common.util.JacksonHelper.MapperType;
import jakarta.inject.Inject;
import jakarta.ws.rs.ext.ContextResolver;
import jakarta.ws.rs.ext.Provider;

/**
 * Provides a properly configured {@link ObjectMapper} used for
 * (de-)serialization of JSON objects.
 */
@Provider
public class JerseyObjectMapper implements ContextResolver {

    @Inject
    private Iterable additionalModules;

    private final ObjectMapper mapper;

    /**
     * Constructor used on the server with injection
     */
    public JerseyObjectMapper() {
        this(Collections.emptyList());
    }

    /**
     * Injection not available in the same way on the client - manual workaround.
     */
    public JerseyObjectMapper(Iterable additional) {
        additionalModules = additional;
        mapper = createMapper();
    }

    @Override
    public ObjectMapper getContext(Class type) {
        return mapper;
    }

    private ObjectMapper createMapper() {
        final ObjectMapper result = JacksonHelper.createObjectMapper(MapperType.JSON);

        for (Module m : additionalModules) {
            result.registerModule(m);
        }

        return result;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy