共计 2909 个字符,预计需要花费 8 分钟才能阅读完成。
【SpringBoot】中工夫类型 序列化、反序列化、格局解决
Date
yml 全局配置
spring:
jackson:
time-zone: GMT+8
date-format: yyyy-MM-dd HH:mm:ss #配置 POST 申请 Body 中 Date 工夫类型序列化格局解决,并返回
申请参数类型转换
/**
* 工夫 Date 转换
* 配置 GET 申请,Query 查问 Date 工夫类型参数转换
*/
@Component
public class DateConverter implements Converter<String, Date> {
@Override
public Date convert(String source) {if (StringUtils.isBlank(source)) {return null;}
if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")) {return parseDate(source.trim(), "yyyy-MM-dd");
}
if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")) {return parseDate(source.trim(), "yyyy-MM-dd HH:mm:ss");
}
throw new IllegalArgumentException("Invalid value'" + source + "'");
}
public Date parseDate(String dateStr, String format) {
Date date = null;
try {date = new SimpleDateFormat(format).parse(dateStr);
} catch (ParseException e) {log.warn("转换 {} 为日期 (pattern={}) 谬误!", dateStr, format);
}
return date;
}
}
JDK8- 工夫类型 -LocalDateTime、LocalDate、LocalTime
/**
* 序列化, 反序列化, 格局解决
*
* @author zc
* @date 2020/7/9 01:42
*/
@Slf4j
@Configuration
public class JacksonCustomizerConfig {@Value("${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}")
private String localDateTimePattern;
@Value("${spring.jackson.local-date-format:yyyy-MM-dd}")
private String localDatePattern;
@Value("${spring.jackson.local-time-format:HH:mm:ss}")
private String localTimePattern;
@Bean
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
return builder -> {builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(localDateTimePattern)));
builder.serializerByType(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(localDatePattern)));
builder.serializerByType(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(localTimePattern)));
builder.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(localDateTimePattern)));
builder.deserializerByType(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(localDatePattern)));
builder.deserializerByType(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(localTimePattern)));
};
}
/**
* 工夫 LocalDateTime 转换
*/
@Component
public static class LocalDateTimeConverter implements Converter<String, LocalDateTime> {
@Override
public LocalDateTime convert(String source) {if (StringUtils.isBlank(source)) {return null;}
if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")) {return LocalDateTime.parse(source, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
throw new IllegalArgumentException("Invalid value'" + source + "'");
}
}
/**
* 工夫 LocalDate 转换
*/
@Component
public static class LocalDateConverter implements Converter<String, LocalDate> {
@Override
public LocalDate convert(String source) {if (StringUtils.isBlank(source)) {return null;}
if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")) {return LocalDate.parse(source, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
}
throw new IllegalArgumentException("Invalid value'" + source + "'");
}
}
}
赵小胖集体博客:https://zc.happyloves.cn:4443/wordpress/
正文完