Quick revision
The short answer
We implement a Converter<String, DateRange>, or a Formatter<DateRange> if we also want to print it, that splits the text on .. and parses both dates, and register it with WebMvcConfigurer.addFormatters(FormatterRegistry); in Spring Boot, a Converter or Formatter bean is picked up automatically. Then @RequestParam DateRange range just works. A Formatter is locale-aware and suits display formatting, while a Converter is plain type conversion, and invalid input should throw an exception so Spring answers with a 400 type mismatch error.
What to remember
In this answer, we defined a DateRange record that enforces its own rule, wrote a Converter<String, DateRange> that parses from..to, and registered it with WebMvcConfigurer.addFormatters, noting that Spring Boot picks up Converter and Formatter beans automatically. We saw that throwing on invalid input gives a 400 type mismatch error, and that a Formatter adds locale-aware parsing and printing for display, while a Converter is plain type conversion.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We implement a Converter<String, DateRange>, or a Formatter<DateRange> if we also want to print it, that splits the text on .. and parses both dates, and register it with WebMvcConfigurer.addFormatters(FormatterRegistry); in Spring Boot, a Converter or Formatter bean is picked up automatically. Then @RequestParam DateRange range just works. A Formatter is locale-aware and suits display formatting, while a Converter is plain type conversion, and invalid input should throw an exception so Spring answers with a 400 type mismatch error.
Why this question is asked
Controllers often receive values that are more than a string or a number: a date range, a money amount, a coordinate. We could take a String and parse it in every controller method, but then the parsing and its error handling are repeated everywhere and the method signature hides what the parameter really is. Spring's conversion system lets us teach it the type once. Interviewers ask for this in a coding round because it shows whether we know where data binding happens and how to plug into it. In this answer, we will define the value type, write the converter, register it, see what happens with bad input, and compare it with a Formatter.
1Defining the DateRange Type
We start with the type the controller should receive. A record is a good fit: it is immutable, and its compact constructor can enforce the rule that a range cannot end before it starts.
Putting the rule in the type means it holds everywhere DateRange is created, not only in the web layer.
2Writing the Converter
A Converter<S, T> is the simplest plug-in of Spring's conversion system: one method that turns a value of type S into a value of type T. Ours splits the text on the two dots and parses each half as an ISO date.
Notice the escaped dots in the regular expression: a plain . would match any character. The -1 limit keeps empty parts, so 2024-01-01.. is rejected instead of silently treated as one part.
3Registering It and Using It in a Controller
Spring MVC binds request parameters with its conversion service, so we add our converter there. In plain Spring MVC we implement WebMvcConfigurer.addFormatters, which receives the FormatterRegistry; despite its name it accepts converters too. In Spring Boot, simply declaring the converter as a bean is enough, because Boot adds every Converter, GenericConverter and Formatter bean to the MVC conversion service.
6 messages in order. Play them, or step through them one at a time.
4What Happens With Invalid Input
When convert throws, Spring wraps the failure in a MethodArgumentTypeMismatchException, a kind of TypeMismatchException, and the default exception handling turns it into 400 Bad Request. That is exactly the right status: the client sent a value we cannot understand.
The important part is that our converter throws. If it returned null for bad input, Spring would treat the parameter as missing, and the client would get a less helpful error or, for an optional parameter, no error at all. With ProblemDetail responses enabled, we can add a @ExceptionHandler for MethodArgumentTypeMismatchException to tell the client the expected format.
5Converter or Formatter?
A Formatter<T> combines a Parser and a Printer, and both methods receive the Locale of the current request. It suits values that are shown to people, where the text form may depend on the language, such as 01.02.2024 in Germany and 2/1/2024 in the US, and where we also want to print the value back, for example into a form field or a link. A Converter is a plain, locale-free type conversion and suits machine formats such as ISO dates in an API.
For a REST API with an ISO format, the Converter is enough. If the same type also appears in server-rendered pages, the Formatter gives us printing for free.
6Follow-up questions
How would you make the same conversion work for @ConfigurationProperties binding?
Configuration properties are bound with Boot's own conversion service, not the MVC one, so a converter registered with addFormatters is not used there. We declare the converter as a bean and annotate it with @ConfigurationPropertiesBinding. Then a property such as acme.report.default-range=2024-01-01..2024-02-01 binds to a DateRange field. Keep such converters free of dependencies on other beans, because they are needed early, while configuration properties are being bound.
Could I use a constructor-bound object instead, with from and to parameters?
Yes, ?from=2024-01-01&to=2024-02-01 binds to a DateRange parameter without @RequestParam through data binding, using the record's constructor. That is a good choice when the API can use two parameters. The converter is the right tool when the format is fixed as a single parameter, as in this question.
Does the converter also work for @PathVariable?
Yes. Path variables, request parameters, headers and form fields are all converted with the same MVC conversion service, so /reports/2024-01-01..2024-02-01 with @PathVariable DateRange range works too.
7Conclusion
In this answer, we defined a DateRange record that enforces its own rule, wrote a Converter<String, DateRange> that parses from..to, and registered it with WebMvcConfigurer.addFormatters, noting that Spring Boot picks up Converter and Formatter beans automatically. We saw that throwing on invalid input gives a 400 type mismatch error, and that a Formatter adds locale-aware parsing and printing for display, while a Converter is plain type conversion.
Open with
First I would model the value as a record, DateRange with from and to as LocalDate, and check in the compact constructor that the range does not end before it starts.
Then, if they want more
Then I write a Converter from String to DateRange. It trims the text, splits it on two literal dots, escaped in the regex and with a negative limit so empty parts are kept, checks there are exactly two parts, and parses each with LocalDate.parse.
To register it in plain Spring MVC, I implement WebMvcConfigurer and add it in addFormatters through the FormatterRegistry; in Spring Boot I can simply declare the converter as a bean, because Boot adds Converter and Formatter beans to the MVC conversion service.
After that, a controller method can take @RequestParam DateRange range, and the argument resolver asks the conversion service to turn the string into a DateRange. For bad input the converter must throw, not return null. Spring wraps the exception in a MethodArgumentTypeMismatchException and answers with 400, which is correct.
As for Converter versus Formatter: a Formatter has parse and print methods that receive the locale, which suits values shown to people and printed back into pages. A Converter is plain, locale-free conversion, which is all an ISO-formatted API parameter needs.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Core technologies
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A request parameter like ?range=2024-01-01..2024-02-01 should bind to a DateRange object...": https://scrivio-interview-library.netlify.app/spring-boot/spring-utilities/2b7e35f7cb/