spring-boot icon indicating copy to clipboard operation
spring-boot copied to clipboard

Document defaults for spring.mvc.format.* and spring.webflux.format.* properties

Open membersound opened this issue 4 years ago • 2 comments

Why are there no default settings for spring.mvc.format.* properties?

I mean: spring is convention over configuration, but when I send a POST request and want spring to parse values like 2021-07-19 into a java.time.LocalDate or java.time.LocalDateTime field, I have to explicit op-in?

Please consider setting them to some reasonable values, like:

#yyyy-MM-dd
spring.mvc.format.date=iso
spring.mvc.format.time=HH:mm:ss
spring.webflux.format.date=...

membersound avatar Jul 19 '21 16:07 membersound

There are defaults but they're not expressed in the metadata at the moment. When the properties aren't configured, your app will use the fallbacks provided by Framework's DateTimeFormatterRegistrar:

private DateTimeFormatter getFallbackFormatter(Type type) {
	switch (type) {
		case DATE: return DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
		case TIME: return DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT);
		default: return DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
	}
}

Changing the defaults would be a breaking change which we generally try to avoid. It may be possible for us to improve the metadata though.

wilkinsona avatar Jul 19 '21 16:07 wilkinsona

Okay, while I feel FormatStyle.SHORT is a rather bad default for date, at least it could be added to the metadata and docs for clarification? Would be nice!

membersound avatar Jul 19 '21 17:07 membersound