feat(web): 新增日期类型转换器

This commit is contained in:
2025-03-20 20:30:23 +08:00
parent c9c7c34506
commit d9ac2764aa
8 changed files with 156 additions and 2 deletions

View File

@@ -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);
/**

View File

@@ -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

View File

@@ -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;

View File

@@ -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;

View File

@@ -0,0 +1,36 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* 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
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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<String, Date> {
@Override
public Date convert(String source) {
return DateUtil.parse(source);
}
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* 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
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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<String, LocalDate> {
@Override
public LocalDate convert(String source) {
return DateUtil.parse(source).toLocalDateTime().toLocalDate();
}
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* 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
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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<String, LocalDateTime> {
@Override
public LocalDateTime convert(String source) {
return DateUtil.parse(source).toLocalDateTime();
}
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* 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
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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<String, LocalTime> {
@Override
public LocalTime convert(String source) {
return DateUtil.parse(source).toLocalDateTime().toLocalTime();
}
}