Learnitweb

Using @JsonComponent in Spring Boot

What Problem Does @JsonComponent Solve?

Before @JsonComponent (Manual Approach)

Before Spring Boot introduced @JsonComponent, if you wanted to register a custom serializer or deserializer with Jackson, you had to manually register it with the ObjectMapper. For example:

@Bean
public ObjectMapper objectMapper() {
    ObjectMapper mapper = new ObjectMapper();
    SimpleModule module = new SimpleModule();
    module.addSerializer(MyClass.class, new MyCustomSerializer());
    mapper.registerModule(module);
    return mapper;
}

This was:

  • Verbose
  • Required manual configuration
  • Not automatically integrated into Spring Boot’s auto-configuration

After @JsonComponent (Automatic Approach)

Spring Boot introduced @JsonComponent to simplify this. Now, you can:

  • Write a serializer/deserializer
  • Annotate it with @JsonComponent
  • Spring Boot will automatically register it with Jackson’s ObjectMapper via classpath scanning

No manual configuration needed.

When to Use

Use @JsonComponent when:

  • You need global serialization/deserialization behavior
  • You want to avoid manual ObjectMapper configuration
  • You prefer clean, Spring Boot idiomatic code

Quick Example

@JsonComponent
public class LocalDateSerializer extends JsonSerializer<LocalDate> {
    public void serialize(LocalDate value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        gen.writeString(value.format(DateTimeFormatter.ofPattern("dd-MM-yyyy")));
    }
}

This will automatically apply to all LocalDate fields in your JSON responses.