男女双方愿意相互观察是爱情的第一征象——瓦西列

对于这种请求:

1
http://api.achao.cn/example?date=2022-11-09

我们可以配置转换器,mvc则会自动帮我们转

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import cn.hutool.core.date.DatePattern;
import io.github.vampireachao.stream.core.optional.Sf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.support.GenericConversionService;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
* @author <achao1441470436@gmail.com>
* @since 2022/4/28 18:30
*/
@Configuration
public class ConvertConfig {

@Autowired
public <T, R> ConvertConfig(ConversionService conversionService, List<Converter<T, R>> converters) {
GenericConversionService genericConversionService = (GenericConversionService) conversionService;
genericConversionService.addConverter(new Converter<String, LocalDate>() {
@Override
public LocalDate convert(String source) {
return Sf.ofStr(source).mayLet(text -> LocalDate.parse(text, DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN))).get();
}
});
genericConversionService.addConverter(new Converter<String, LocalDateTime>() {
@Override
public LocalDateTime convert(String source) {
return Sf.ofStr(source).mayLet(text -> LocalDateTime.parse(text, DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN))).get();
}
});
converters.forEach(genericConversionService::addConverter);
}


}