admin管理员组

文章数量:1122846

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):

  • Class which extends jakarta.ws.rs.core.Application
  • Class with @Path and other associated annotations for the endpoints
  • beans.xml and persistence.xml (though not using JPA yet)
  • I'm using org.jboss.weld.se:weld-se-core v6.0.0.Beta4 for CDI.

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);

本文标签: jerseyHow do you provide an ObjectMapper (or JsonMapper) in tomcat 11Stack Overflow