mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-10 08:57:19 +08:00
feat(web): 新增日期类型转换器
This commit is contained in:
@@ -43,6 +43,7 @@ import top.continew.starter.core.constant.StringConstants;
|
|||||||
@ConditionalOnProperty(prefix = PropertiesConstants.WEB_CORS, name = PropertiesConstants.ENABLED, havingValue = "true")
|
@ConditionalOnProperty(prefix = PropertiesConstants.WEB_CORS, name = PropertiesConstants.ENABLED, havingValue = "true")
|
||||||
@EnableConfigurationProperties(CorsProperties.class)
|
@EnableConfigurationProperties(CorsProperties.class)
|
||||||
public class CorsAutoConfiguration {
|
public class CorsAutoConfiguration {
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(CorsAutoConfiguration.class);
|
private static final Logger log = LoggerFactory.getLogger(CorsAutoConfiguration.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -26,6 +26,11 @@ import org.springframework.http.converter.HttpMessageConverter;
|
|||||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
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.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
@@ -70,6 +75,10 @@ public class WebMvcAutoConfiguration implements WebMvcConfigurer {
|
|||||||
@Override
|
@Override
|
||||||
public void addFormatters(FormatterRegistry registry) {
|
public void addFormatters(FormatterRegistry registry) {
|
||||||
registry.addConverterFactory(new BaseEnumConverterFactory());
|
registry.addConverterFactory(new BaseEnumConverterFactory());
|
||||||
|
registry.addConverter(new DateConverter());
|
||||||
|
registry.addConverter(new LocalDateTimeConverter());
|
||||||
|
registry.addConverter(new LocalDateConverter());
|
||||||
|
registry.addConverter(new LocalTimeConverter());
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
|
@@ -14,7 +14,7 @@
|
|||||||
* limitations under the License.
|
* 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.Converter;
|
||||||
import top.continew.starter.core.enums.BaseEnum;
|
import top.continew.starter.core.enums.BaseEnum;
|
@@ -14,7 +14,7 @@
|
|||||||
* limitations under the License.
|
* 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.Converter;
|
||||||
import org.springframework.core.convert.converter.ConverterFactory;
|
import org.springframework.core.convert.converter.ConverterFactory;
|
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user