Mastering Date and Time Parameter Conversion in Spring MVC
This guide explains how to handle date and time parameters in Spring MVC by using @DateTimeFormat for request‑level conversion, configuring global formatters via WebMvcAutoConfiguration, and implementing custom Converter, GenericConverter, and Formatter beans to ensure seamless ISO‑8601 parsing.
Overview
In a recent mini‑program, several Spring MVC endpoints received date values directly using Java 8 time APIs. When ISO‑8601 formatted strings were sent via POST, Spring threw an exception because, by default, it cannot convert a String to date or time objects. This article demonstrates how to perform type conversion and formatting in Spring MVC (the same mechanism applies to WebFlux).
Converting Date Parameters at the Request Level
One solution is to annotate the method parameter with @DateTimeFormat and provide a pattern. This allows ISO‑8601 strings to be automatically converted to date objects, and you can also define a custom pattern.
Globally Converting Date Parameters in the Application
Another approach is to provide a global configuration. Spring’s WebMvcAutoConfiguration contains the relevant handling mechanism. By specifying a format in this configuration, Spring can automatically convert date strings throughout the application.
The key is the parameters of the class, which register a unified handling style. During initialization, FormattingConversionService calls addFormatters(FormatterRegistry), whose implementation can be examined in the source.
Spring can automatically inject beans of type Converter, GenericConverter, and Formatter into the formatting registry, provided they are registered as Spring beans.
Converters
Converter<S,T>is a functional interface that converts a source type S to a target type T. It can be used for conversions such as String to Integer or String to LocalDate. Implementations must be thread‑safe and should not accept null source values. For centralized conversion needs, you can implement ConverterFactory.
GenericConverteris a more flexible interface that can convert between two or more types. By defining multiple ConvertiblePair mappings, it supports one‑to‑many and many‑to‑many conversion relationships.
Formatters
Formatter<T>is a typed interface for implementing field formatting logic. It combines Printer<T>, which converts an object to a display string, and Parser<T>, which parses a string back to an object, optionally supporting localization. For example, it can format a yyyy‑MM‑dd string into a LocalDate.
Summary
We learned how to accept date parameters in Spring MVC requests, both at the method level using @DateTimeFormat and globally via configuration. By implementing and registering Converter, GenericConverter, and Formatter beans, Spring can automatically handle the conversion and formatting of date and time objects.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
