From d9ac2764aa78e83a11ee5440155a8cd7bf1cb8c8 Mon Sep 17 00:00:00 2001 From: Charles7c Date: Thu, 20 Mar 2025 20:30:23 +0800 Subject: [PATCH] =?UTF-8?q?feat(web):=20=E6=96=B0=E5=A2=9E=E6=97=A5?= =?UTF-8?q?=E6=9C=9F=E7=B1=BB=E5=9E=8B=E8=BD=AC=E6=8D=A2=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cors/CorsAutoConfiguration.java | 1 + .../mvc/WebMvcAutoConfiguration.java | 9 +++++ .../{ => converter}/BaseEnumConverter.java | 2 +- .../BaseEnumConverterFactory.java | 2 +- .../mvc/converter/time/DateConverter.java | 36 +++++++++++++++++++ .../converter/time/LocalDateConverter.java | 36 +++++++++++++++++++ .../time/LocalDateTimeConverter.java | 36 +++++++++++++++++++ .../converter/time/LocalTimeConverter.java | 36 +++++++++++++++++++ 8 files changed, 156 insertions(+), 2 deletions(-) rename continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/mvc/{ => converter}/BaseEnumConverter.java (95%) rename continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/mvc/{ => converter}/BaseEnumConverterFactory.java (95%) create mode 100644 continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/mvc/converter/time/DateConverter.java create mode 100644 continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/mvc/converter/time/LocalDateConverter.java create mode 100644 continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/mvc/converter/time/LocalDateTimeConverter.java create mode 100644 continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/mvc/converter/time/LocalTimeConverter.java diff --git a/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/cors/CorsAutoConfiguration.java b/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/cors/CorsAutoConfiguration.java index 6d5b015d..639e8df2 100644 --- a/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/cors/CorsAutoConfiguration.java +++ b/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/cors/CorsAutoConfiguration.java @@ -43,6 +43,7 @@ import top.continew.starter.core.constant.StringConstants; @ConditionalOnProperty(prefix = PropertiesConstants.WEB_CORS, name = PropertiesConstants.ENABLED, havingValue = "true") @EnableConfigurationProperties(CorsProperties.class) public class CorsAutoConfiguration { + private static final Logger log = LoggerFactory.getLogger(CorsAutoConfiguration.class); /** diff --git a/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/mvc/WebMvcAutoConfiguration.java b/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/mvc/WebMvcAutoConfiguration.java index ad1e434f..c332bc92 100644 --- a/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/mvc/WebMvcAutoConfiguration.java +++ b/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/mvc/WebMvcAutoConfiguration.java @@ -26,6 +26,11 @@ import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import top.continew.starter.web.autoconfigure.mvc.converter.BaseEnumConverterFactory; +import top.continew.starter.web.autoconfigure.mvc.converter.time.DateConverter; +import top.continew.starter.web.autoconfigure.mvc.converter.time.LocalDateConverter; +import top.continew.starter.web.autoconfigure.mvc.converter.time.LocalDateTimeConverter; +import top.continew.starter.web.autoconfigure.mvc.converter.time.LocalTimeConverter; import java.util.List; import java.util.Objects; @@ -70,6 +75,10 @@ public class WebMvcAutoConfiguration implements WebMvcConfigurer { @Override public void addFormatters(FormatterRegistry registry) { registry.addConverterFactory(new BaseEnumConverterFactory()); + registry.addConverter(new DateConverter()); + registry.addConverter(new LocalDateTimeConverter()); + registry.addConverter(new LocalDateConverter()); + registry.addConverter(new LocalTimeConverter()); } @PostConstruct diff --git a/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/mvc/BaseEnumConverter.java b/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/mvc/converter/BaseEnumConverter.java similarity index 95% rename from continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/mvc/BaseEnumConverter.java rename to continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/mvc/converter/BaseEnumConverter.java index 7e2d3aea..5b99a594 100644 --- a/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/mvc/BaseEnumConverter.java +++ b/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/mvc/converter/BaseEnumConverter.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package top.continew.starter.web.autoconfigure.mvc; +package top.continew.starter.web.autoconfigure.mvc.converter; import org.springframework.core.convert.converter.Converter; import top.continew.starter.core.enums.BaseEnum; diff --git a/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/mvc/BaseEnumConverterFactory.java b/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/mvc/converter/BaseEnumConverterFactory.java similarity index 95% rename from continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/mvc/BaseEnumConverterFactory.java rename to continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/mvc/converter/BaseEnumConverterFactory.java index 20a64092..bad2e878 100644 --- a/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/mvc/BaseEnumConverterFactory.java +++ b/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/mvc/converter/BaseEnumConverterFactory.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package top.continew.starter.web.autoconfigure.mvc; +package top.continew.starter.web.autoconfigure.mvc.converter; import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.converter.ConverterFactory; diff --git a/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/mvc/converter/time/DateConverter.java b/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/mvc/converter/time/DateConverter.java new file mode 100644 index 00000000..c33b6e5d --- /dev/null +++ b/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/mvc/converter/time/DateConverter.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * 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 top.continew.starter.web.autoconfigure.mvc.converter.time; + +import cn.hutool.core.date.DateUtil; +import org.springframework.core.convert.converter.Converter; + +import java.util.Date; + +/** + * Date 参数转换器 + * + * @author Charles7c + * @since 2.10.0 + */ +public class DateConverter implements Converter { + + @Override + public Date convert(String source) { + return DateUtil.parse(source); + } +} diff --git a/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/mvc/converter/time/LocalDateConverter.java b/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/mvc/converter/time/LocalDateConverter.java new file mode 100644 index 00000000..8133e86e --- /dev/null +++ b/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/mvc/converter/time/LocalDateConverter.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * 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 top.continew.starter.web.autoconfigure.mvc.converter.time; + +import cn.hutool.core.date.DateUtil; +import org.springframework.core.convert.converter.Converter; + +import java.time.LocalDate; + +/** + * LocalDate 参数转换器 + * + * @author Charles7c + * @since 2.10.0 + */ +public class LocalDateConverter implements Converter { + + @Override + public LocalDate convert(String source) { + return DateUtil.parse(source).toLocalDateTime().toLocalDate(); + } +} diff --git a/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/mvc/converter/time/LocalDateTimeConverter.java b/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/mvc/converter/time/LocalDateTimeConverter.java new file mode 100644 index 00000000..3e5ff140 --- /dev/null +++ b/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/mvc/converter/time/LocalDateTimeConverter.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * 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 top.continew.starter.web.autoconfigure.mvc.converter.time; + +import cn.hutool.core.date.DateUtil; +import org.springframework.core.convert.converter.Converter; + +import java.time.LocalDateTime; + +/** + * LocalDateTime 参数转换器 + * + * @author Charles7c + * @since 2.10.0 + */ +public class LocalDateTimeConverter implements Converter { + + @Override + public LocalDateTime convert(String source) { + return DateUtil.parse(source).toLocalDateTime(); + } +} diff --git a/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/mvc/converter/time/LocalTimeConverter.java b/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/mvc/converter/time/LocalTimeConverter.java new file mode 100644 index 00000000..1f8fd744 --- /dev/null +++ b/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/mvc/converter/time/LocalTimeConverter.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * 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 top.continew.starter.web.autoconfigure.mvc.converter.time; + +import cn.hutool.core.date.DateUtil; +import org.springframework.core.convert.converter.Converter; + +import java.time.LocalTime; + +/** + * LocalTime 参数转换器 + * + * @author Charles7c + * @since 2.10.0 + */ +public class LocalTimeConverter implements Converter { + + @Override + public LocalTime convert(String source) { + return DateUtil.parse(source).toLocalDateTime().toLocalTime(); + } +}