Home > Blockchain >  (Why) is Jdk8Module registering "itself"?
(Why) is Jdk8Module registering "itself"?

Time:08-27

I have an implementation using Tomcat v10.0.x and org.glassfish.jersey.* in version 3.0.5 to provide a web service. Initially, I had problems returning response Objects as MediaType.APPLICATION_JSON whenever they hat Attributes/Getters that are Optional.

Once I added jackson-datatype-jdk8 2.13.3 based on this question, everything started to work fine.

I am confused, though. I did not register Jdk8Module because I am not actively creating a Client instance. As far as I believe to understand the inner workings of Jersey as JAX-RS implementation, the @POST annotations I am using are responsible for handling Client objects. It might also be Tomcat. On the web there is a big gap between simple howtos with little explanation, and in-depth documentation that is hard to understand.

Where is Jdk8Module registered, and how, and why, or not at all? Simply speaking, why does my code work?

CodePudding user response:

Short answer: META-INF/services.

Longer answer: Jackson defines Module class which is used as a service (defined in META-INF/services) loaded by a standard ServiceLoader mechanism. Jackson defines a static method loading all Modules and the Jackson jackson-datatype-java8 module defines the proper service.

Jersey just uses the Jackson service-finding routine to register the module.

  • Related