io.bdeploy.jersey.JerseyObjectMapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of api Show documentation
Show all versions of api Show documentation
Public API including dependencies, ready to be used for integrations and plugins.
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