Reputation: 349
I'm trying to get a query param of the following format "2018-01" to be used as a YearMonth (java.time) object.
In my controller, I have the following param:
@RequestParam(value = "start") @DateTimeFormat(pattern = "yyyy-MM") YearMonth start
but I get the following error: Failed to convert value of type [java.lang.String] to required type [java.time.YearMonth]; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.time.YearMonth]: no matching editors or conversion strategy found
Edit:
According to this link: https://jira.spring.io/browse/SPR-13518 , the java.time.YearMonth conversion is supported since version 4.2.4, and I'm using spring version 4.3.3.RELEASE
Upvotes: 3
Views: 2883
Reputation: 3325
Don't know if YearMonth is supported by the @DateTimeFormatter annotation for implicit conversion. But
you can always registered custom Converter for this YearMonth type like this:
@Configuration
@EnableMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new YearMonthConverter());
}
}
with your YearMonthConverter defined as:
public class YearMonthConverter implements Converter<String, YearMonth> {
@Override
public YearMonth convert(String text) {
return YearMonth.parse(text, DateTimeFormatter.ofPattern("yyyy-MM"));
}
}
there you are assured of implicit conversion.
Upvotes: 3