• SpringBoot接收LocalDateTime参数


    一、通过@RequestBody接收

    方式1:实体类上加上 @JsonFormat,并通过 pattern 属性指定时间格式

    1. public class Time {
    2. @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    3. LocalDateTime localDateTime;
    4. @JsonFormat(pattern = "yyyy-MM-dd")
    5. LocalDate localDate;
    6. @JsonFormat(pattern = "HH:mm:ss")
    7. LocalTime localTime;
    8. }

    方式二:配置Jackson时间序列化,创建如下配置类即可

    1. import com.fasterxml.jackson.databind.ObjectMapper;
    2. import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
    3. import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
    4. import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
    5. import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
    6. import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
    7. import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
    8. import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
    9. import org.springframework.context.annotation.Bean;
    10. import org.springframework.context.annotation.Configuration;
    11. import java.time.LocalDate;
    12. import java.time.LocalDateTime;
    13. import java.time.LocalTime;
    14. import java.time.format.DateTimeFormatter;
    15. @Configuration
    16. public class JacksonConfig {
    17. @Bean
    18. public ObjectMapper objectMapper() {
    19. JavaTimeModule javaTimeModule = new JavaTimeModule();
    20. // 时间序列化
    21. javaTimeModule.addSerializer(LocalDateTime.class,new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    22. javaTimeModule.addSerializer(LocalDate.class,new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
    23. javaTimeModule.addSerializer(LocalTime.class,new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
    24. // 时间反序列化
    25. javaTimeModule.addDeserializer(LocalDateTime.class,new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    26. javaTimeModule.addDeserializer(LocalDate.class,new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
    27. javaTimeModule.addDeserializer(LocalTime.class,new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
    28. ObjectMapper objectMapper = new ObjectMapper();
    29. objectMapper.registerModule(javaTimeModule);
    30. return objectMapper;
    31. }
    32. }

    二、通过@RequestParam接收

    方式1:在参数前面加上 @DateTimeFormat 注解,通过 pattern 属性指定时间格式

    1. @GetMapping("/param/local")
    2. public void testLocalDateTime(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime localDateTime,
    3. @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate localDate,
    4. @RequestParam @DateTimeFormat(pattern = "HH:mm:ss") LocalTime localTime) {
    5. }

    方式2:配置时间格式转换器,创建如下配置类即可

    1. import org.springframework.context.annotation.Bean;
    2. import org.springframework.context.annotation.Configuration;
    3. import org.springframework.core.convert.converter.Converter;
    4. import java.time.LocalDate;
    5. import java.time.LocalDateTime;
    6. import java.time.LocalTime;
    7. import java.time.format.DateTimeFormatter;
    8. /**
    9. * 日期格式转换器配置
    10. */
    11. @Configuration
    12. public class DateConverterConfig {
    13. /**
    14. * LocalDateTime格式转换器
    15. */
    16. @Bean
    17. public Converter localDateTimeConverter() {
    18. return new Converter() {
    19. @Override
    20. public LocalDateTime convert(String timeStr) {
    21. return LocalDateTime.parse(timeStr,DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    22. }
    23. };
    24. }
    25. /**
    26. * LocalDate格式转换器
    27. */
    28. @Bean
    29. public Converter localDateConverter() {
    30. return new Converter() {
    31. @Override
    32. public LocalDate convert(String timeStr) {
    33. return LocalDate.parse(timeStr,DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    34. }
    35. };
    36. }
    37. /**
    38. * LocalTime格式转换器
    39. */
    40. @Bean
    41. public Converter localTimeConverter() {
    42. return new Converter() {
    43. @Override
    44. public LocalTime convert(String timeStr) {
    45. return LocalTime.parse(timeStr,DateTimeFormatter.ofPattern("HH:mm:ss"));
    46. }
    47. };
    48. }
    49. }

     

  • 相关阅读:
    openpnp - 底部相机高级矫正后,底部相机看不清吸嘴的解决方法
    smallWhiteDot Tech Suppor
    企业应用超融合架构的设计实例及超融合应用场景分析
    一文弄懂 if __name__ == “__main__“:(洒洒水啦!)
    【Excel】两组行数不同数据做二维柱状图
    python实现冒泡排序
    快排 记忆 口诀
    SAR信号处理基础2——线性调频信号频谱与驻定相位原理
    flutter 系列之:flutter 中的幽灵offstage
    使用Mac的Applescript定制sublime执行C/C++程序弹出终端
  • 原文地址:https://blog.csdn.net/weixin_74261199/article/details/140408343