Create spring-boot-webmvc module

This commit is contained in:
Andy Wilkinson
2025-03-27 14:05:55 +00:00
committed by Phillip Webb
parent e83f4eed4e
commit dfd5e56956
129 changed files with 450 additions and 360 deletions

View File

@@ -0,0 +1,113 @@
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.web.format;
import java.time.format.DateTimeFormatter;
import java.time.format.ResolverStyle;
import org.springframework.util.StringUtils;
/**
* {@link DateTimeFormatter Formatters} for dates, times, and date-times.
*
* @author Andy Wilkinson
* @author Gaurav Pareek
* @since 2.3.0
*/
public class DateTimeFormatters {
private DateTimeFormatter dateFormatter;
private String datePattern;
private DateTimeFormatter timeFormatter;
private DateTimeFormatter dateTimeFormatter;
/**
* Configures the date format using the given {@code pattern}.
* @param pattern the pattern for formatting dates
* @return {@code this} for chained method invocation
*/
public DateTimeFormatters dateFormat(String pattern) {
if (isIso(pattern)) {
this.dateFormatter = DateTimeFormatter.ISO_LOCAL_DATE;
this.datePattern = "yyyy-MM-dd";
}
else {
this.dateFormatter = formatter(pattern);
this.datePattern = pattern;
}
return this;
}
/**
* Configures the time format using the given {@code pattern}.
* @param pattern the pattern for formatting times
* @return {@code this} for chained method invocation
*/
public DateTimeFormatters timeFormat(String pattern) {
this.timeFormatter = isIso(pattern) ? DateTimeFormatter.ISO_LOCAL_TIME
: (isIsoOffset(pattern) ? DateTimeFormatter.ISO_OFFSET_TIME : formatter(pattern));
return this;
}
/**
* Configures the date-time format using the given {@code pattern}.
* @param pattern the pattern for formatting date-times
* @return {@code this} for chained method invocation
*/
public DateTimeFormatters dateTimeFormat(String pattern) {
this.dateTimeFormatter = isIso(pattern) ? DateTimeFormatter.ISO_LOCAL_DATE_TIME
: (isIsoOffset(pattern) ? DateTimeFormatter.ISO_OFFSET_DATE_TIME : formatter(pattern));
return this;
}
DateTimeFormatter getDateFormatter() {
return this.dateFormatter;
}
String getDatePattern() {
return this.datePattern;
}
DateTimeFormatter getTimeFormatter() {
return this.timeFormatter;
}
DateTimeFormatter getDateTimeFormatter() {
return this.dateTimeFormatter;
}
boolean isCustomized() {
return this.dateFormatter != null || this.timeFormatter != null || this.dateTimeFormatter != null;
}
private static DateTimeFormatter formatter(String pattern) {
return StringUtils.hasText(pattern)
? DateTimeFormatter.ofPattern(pattern).withResolverStyle(ResolverStyle.SMART) : null;
}
private static boolean isIso(String pattern) {
return "iso".equalsIgnoreCase(pattern);
}
private static boolean isIsoOffset(String pattern) {
return "isooffset".equalsIgnoreCase(pattern) || "iso-offset".equalsIgnoreCase(pattern);
}
}

View File

@@ -0,0 +1,99 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.web.format;
import java.time.format.DateTimeFormatter;
import java.util.function.Consumer;
import java.util.function.Supplier;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.format.datetime.DateFormatterRegistrar;
import org.springframework.format.datetime.standard.DateTimeFormatterRegistrar;
import org.springframework.format.number.NumberFormatAnnotationFormatterFactory;
import org.springframework.format.number.money.CurrencyUnitFormatter;
import org.springframework.format.number.money.Jsr354NumberFormatAnnotationFormatterFactory;
import org.springframework.format.number.money.MonetaryAmountFormatter;
import org.springframework.format.support.DefaultFormattingConversionService;
import org.springframework.util.ClassUtils;
/**
* {@link org.springframework.format.support.FormattingConversionService} dedicated to web
* applications for formatting and converting values to/from the web.
*
* @author Brian Clozel
* @since 2.0.0
*/
public class WebConversionService extends DefaultFormattingConversionService {
private static final boolean JSR_354_PRESENT = ClassUtils.isPresent("javax.money.MonetaryAmount",
WebConversionService.class.getClassLoader());
/**
* Create a new WebConversionService that configures formatters with the provided
* date, time, and date-time formats, or registers the default if no custom format is
* provided.
* @param dateTimeFormatters the formatters to use for date, time, and date-time
* formatting
* @since 2.3.0
*/
public WebConversionService(DateTimeFormatters dateTimeFormatters) {
super(false);
if (dateTimeFormatters.isCustomized()) {
addFormatters(dateTimeFormatters);
}
else {
addDefaultFormatters(this);
}
}
private void addFormatters(DateTimeFormatters dateTimeFormatters) {
addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory());
if (JSR_354_PRESENT) {
addFormatter(new CurrencyUnitFormatter());
addFormatter(new MonetaryAmountFormatter());
addFormatterForFieldAnnotation(new Jsr354NumberFormatAnnotationFormatterFactory());
}
registerJsr310(dateTimeFormatters);
registerJavaDate(dateTimeFormatters);
}
private void registerJsr310(DateTimeFormatters dateTimeFormatters) {
DateTimeFormatterRegistrar dateTime = new DateTimeFormatterRegistrar();
configure(dateTimeFormatters::getDateFormatter, dateTime::setDateFormatter);
configure(dateTimeFormatters::getTimeFormatter, dateTime::setTimeFormatter);
configure(dateTimeFormatters::getDateTimeFormatter, dateTime::setDateTimeFormatter);
dateTime.registerFormatters(this);
}
private void configure(Supplier<DateTimeFormatter> supplier, Consumer<DateTimeFormatter> consumer) {
DateTimeFormatter formatter = supplier.get();
if (formatter != null) {
consumer.accept(formatter);
}
}
private void registerJavaDate(DateTimeFormatters dateTimeFormatters) {
DateFormatterRegistrar dateFormatterRegistrar = new DateFormatterRegistrar();
String datePattern = dateTimeFormatters.getDatePattern();
if (datePattern != null) {
DateFormatter dateFormatter = new DateFormatter(datePattern);
dateFormatterRegistrar.setFormatter(dateFormatter);
}
dateFormatterRegistrar.registerFormatters(this);
}
}

View File

@@ -0,0 +1,20 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Support classes for web-specific formatting.
*/
package org.springframework.boot.autoconfigure.web.format;