
io.bdeploy.jersey.JerseyObjectMapper Maven / Gradle / Ivy
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 - 2025 Weber Informatics LLC | Privacy Policy