Skip to content

Commit d9ac276

Browse files
committed
feat(web): 新增日期类型转换器
1 parent c9c7c34 commit d9ac276

File tree

8 files changed

+156
-2
lines changed

8 files changed

+156
-2
lines changed

continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/cors/CorsAutoConfiguration.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
@ConditionalOnProperty(prefix = PropertiesConstants.WEB_CORS, name = PropertiesConstants.ENABLED, havingValue = "true")
4444
@EnableConfigurationProperties(CorsProperties.class)
4545
public class CorsAutoConfiguration {
46+
4647
private static final Logger log = LoggerFactory.getLogger(CorsAutoConfiguration.class);
4748

4849
/**

continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/mvc/WebMvcAutoConfiguration.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
2727
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
2828
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
29+
import top.continew.starter.web.autoconfigure.mvc.converter.BaseEnumConverterFactory;
30+
import top.continew.starter.web.autoconfigure.mvc.converter.time.DateConverter;
31+
import top.continew.starter.web.autoconfigure.mvc.converter.time.LocalDateConverter;
32+
import top.continew.starter.web.autoconfigure.mvc.converter.time.LocalDateTimeConverter;
33+
import top.continew.starter.web.autoconfigure.mvc.converter.time.LocalTimeConverter;
2934

3035
import java.util.List;
3136
import java.util.Objects;
@@ -70,6 +75,10 @@ public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
7075
@Override
7176
public void addFormatters(FormatterRegistry registry) {
7277
registry.addConverterFactory(new BaseEnumConverterFactory());
78+
registry.addConverter(new DateConverter());
79+
registry.addConverter(new LocalDateTimeConverter());
80+
registry.addConverter(new LocalDateConverter());
81+
registry.addConverter(new LocalTimeConverter());
7382
}
7483

7584
@PostConstruct
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package top.continew.starter.web.autoconfigure.mvc;
17+
package top.continew.starter.web.autoconfigure.mvc.converter;
1818

1919
import org.springframework.core.convert.converter.Converter;
2020
import top.continew.starter.core.enums.BaseEnum;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package top.continew.starter.web.autoconfigure.mvc;
17+
package top.continew.starter.web.autoconfigure.mvc.converter;
1818

1919
import org.springframework.core.convert.converter.Converter;
2020
import org.springframework.core.convert.converter.ConverterFactory;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
3+
* <p>
4+
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.gnu.org/licenses/lgpl.html
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package top.continew.starter.web.autoconfigure.mvc.converter.time;
18+
19+
import cn.hutool.core.date.DateUtil;
20+
import org.springframework.core.convert.converter.Converter;
21+
22+
import java.util.Date;
23+
24+
/**
25+
* Date 参数转换器
26+
*
27+
* @author Charles7c
28+
* @since 2.10.0
29+
*/
30+
public class DateConverter implements Converter<String, Date> {
31+
32+
@Override
33+
public Date convert(String source) {
34+
return DateUtil.parse(source);
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
3+
* <p>
4+
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.gnu.org/licenses/lgpl.html
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package top.continew.starter.web.autoconfigure.mvc.converter.time;
18+
19+
import cn.hutool.core.date.DateUtil;
20+
import org.springframework.core.convert.converter.Converter;
21+
22+
import java.time.LocalDate;
23+
24+
/**
25+
* LocalDate 参数转换器
26+
*
27+
* @author Charles7c
28+
* @since 2.10.0
29+
*/
30+
public class LocalDateConverter implements Converter<String, LocalDate> {
31+
32+
@Override
33+
public LocalDate convert(String source) {
34+
return DateUtil.parse(source).toLocalDateTime().toLocalDate();
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
3+
* <p>
4+
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.gnu.org/licenses/lgpl.html
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package top.continew.starter.web.autoconfigure.mvc.converter.time;
18+
19+
import cn.hutool.core.date.DateUtil;
20+
import org.springframework.core.convert.converter.Converter;
21+
22+
import java.time.LocalDateTime;
23+
24+
/**
25+
* LocalDateTime 参数转换器
26+
*
27+
* @author Charles7c
28+
* @since 2.10.0
29+
*/
30+
public class LocalDateTimeConverter implements Converter<String, LocalDateTime> {
31+
32+
@Override
33+
public LocalDateTime convert(String source) {
34+
return DateUtil.parse(source).toLocalDateTime();
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
3+
* <p>
4+
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.gnu.org/licenses/lgpl.html
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package top.continew.starter.web.autoconfigure.mvc.converter.time;
18+
19+
import cn.hutool.core.date.DateUtil;
20+
import org.springframework.core.convert.converter.Converter;
21+
22+
import java.time.LocalTime;
23+
24+
/**
25+
* LocalTime 参数转换器
26+
*
27+
* @author Charles7c
28+
* @since 2.10.0
29+
*/
30+
public class LocalTimeConverter implements Converter<String, LocalTime> {
31+
32+
@Override
33+
public LocalTime convert(String source) {
34+
return DateUtil.parse(source).toLocalDateTime().toLocalTime();
35+
}
36+
}

0 commit comments

Comments
 (0)