William Speirs
William Speirs

Reputation: 1

How do you provide an ObjectMapper (or JsonMapper) in tomcat 11

I have a very simple REST app using JAX-RS, Tomcat 11, and Weld for CDI. It is pretty straightforward with only the following (ie no web.xml or context.xml):

In the class that defines the endpoints, I want to return an object that has a ZonedDateTime field. I followed the directions to findAndAddModules() in a @Produces class:

@ApplicationScoped
public class SerializerConfig {
    private static final Logger logger = Logger.getLogger(SerializerConfig.class.getName());

    @Produces
    public JsonMapper objectMapper() {
        logger.info("PROVIDING!!!");

        return JsonMapper.builder()
                .findAndAddModules()
                .build();
    }
}

This class is never called (don't see "PROVIDING!!!" in my logs) when I simply return Response.ok(obj_with_date_time).build();.

How can I get Weld to provider my instance of JsonMapper to whatever Jersey code is called when returning Response.ok(obj_with_date_time).build();?

My provider is called if I specifically @Inject JsonMapper as a field in my class, and manually perform the serialization: jsonMapper.writeValueAsString(obj_with_date_time);

Upvotes: 0

Views: 18

Answers (0)

Related Questions