title: Is it possible to configure Jackson custom deserializers at class level for different data types?
body: <p>I need to deserialize a long and complex json for which I wrote a set of java classes to map the data, and I had to write custom deserializers for many fields of different types <em>(including String, Boolean, BigDecimal, etc.)</em>.</p>
  
  <p>I know I can annotate all fields in the java classes with the corresponding custom deserializer (like below), but then I would need to annotate almost all the fields in all the classes.</p>
  
  <pre><code>@JsonDeserialize(using = CustomBooleanJsonDeserializer.class)
  private boolean active;
  </code></pre>
  
  <p>I also know that I can register a module in the Spring default <code>ObjectMapper</code> (like <a href="https://stackoverflow.com/a/53481353/4071001">here</a>), but I just want to use these custom deserializers for these specific classes.</p>
  
  <pre><code>@Bean
  public Module customDeserializersModule() {
      SimpleModule module = new SimpleModule();
      module.addDeserializer(Boolean.class, new CustomBooleanJsonDeserializer());
      // add other custom deserializers 
      return module;
  }
  </code></pre>
  
  <p>I even know that I can use a custom <code>ObjectMapper</code> in the <code>RestController</code>, but I don't want to give up the convenience of automatic data binding via <code>@RequestBody</code>, because I must prevent others from using this without the necessary custom deserializers.</p>
  
  <pre><code>@RequestMapping(method = RequestMethod.POST, value = "/data")
  public ResponseEntity&lt;ServerInfo&gt; register(@RequestBody DataMapper data) {
     // DataMapper is the target POJO class of the json's deserialization
  }
  </code></pre>
  
  <hr>
  
  <p><strong>In short, I'm looking for something like this at class level:</strong></p>
  
  <pre><code>@JsonDeserialize(using = CustomStringJsonDeserializer.class, forType = String.class)
  @JsonDeserialize(using = CustomBooleanJsonDeserializer.class, forType = Boolean.class)
  @JsonDeserialize(using = CustomBigDecimalJsonDeserializer.class, forType = BigDecimal.class)
  public class DataMapper implements Serializable {
      // obviously, @JsonDeserialize doesn't have a forType method
  }
  </code></pre>
  
  <p>or maybe some way to <strong>implement a custom deserializer</strong> for the <code>DataMapper</code> class, that defines how to deserialize each field according to its data type (without having to annotate each field):</p>
  
  <pre><code>@JsonDeserialize(using = DataMapperJsonDeserializer.class)
  public class DataMapper implements Serializable {
      // How can I implement the DataMapperJsonDeserializer with these 
      // characteristics? I know about the ContextualDeserializer interface, 
      // but I don't know how to use it without annotating each field.
  }
  </code></pre>
  
  <p>or some way of <strong>restricting the effect of a module to just one package or set of classes</strong>:</p>
  
  <pre><code>module.restrictedTo(/*some package or set of classes*/);
  // com.fasterxml.jackson.databind.Module doesn't have a restrictedTo method
  </code></pre>