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

nl.vpro.jackson2.rs.JacksonContextResolver Maven / Gradle / Ivy

There is a newer version: 5.3.2
Show newest version
package nl.vpro.jackson2.rs;

import jakarta.annotation.Priority;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.ext.ContextResolver;
import jakarta.ws.rs.ext.Provider;

import java.util.function.Supplier;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.jakarta.rs.json.JacksonXmlBindJsonProvider;

import nl.vpro.jackson2.Jackson2Mapper;

/**
 * This is used to bind our object mapper to resteasy/jaxrs.
 * 

* The exposed mapper is actually in a thread local. Some interceptor may influence it. * * @author Michiel Meeuwissen * @since 2.0 */ @Provider @Consumes({MediaType.APPLICATION_JSON, "application/*+json", "text/json"}) @Produces({MediaType.APPLICATION_JSON, "application/*+json", "text/json"}) @Priority(JacksonContextResolver.PRIORITY) public class JacksonContextResolver extends JacksonXmlBindJsonProvider implements ContextResolver { static final int PRIORITY = Priorities.USER; private final ThreadLocal mapper; public JacksonContextResolver() { this(Jackson2Mapper.getLenientInstance()); } public JacksonContextResolver(ObjectMapper mapper) { this(() -> mapper); } public JacksonContextResolver(Supplier mapper) { this.mapper = ThreadLocal.withInitial(mapper); } @Override public ObjectMapper getContext(Class objectType) { return mapper.get(); } /** * @since 4.0 */ public void set(ObjectMapper mapper) { this.mapper.set(mapper); } /** * @since 4.0 */ public void reset() { mapper.remove(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy