java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot LocalDateTime参数

SpringBoot接收LocalDateTime参数的方式

作者:小小李程序员

这篇文章主要介绍了SpringBoot接收LocalDateTime参数的方式,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧

一、通过@RequestBody接收

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

public class Time {
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    LocalDateTime localDateTime;
    @JsonFormat(pattern = "yyyy-MM-dd")
    LocalDate localDate;
    @JsonFormat(pattern = "HH:mm:ss")
    LocalTime localTime;
}

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

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
@Configuration
public class JacksonConfig {
    @Bean
    public ObjectMapper objectMapper() {
        JavaTimeModule javaTimeModule = new JavaTimeModule();
        //  时间序列化
        javaTimeModule.addSerializer(LocalDateTime.class,new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        javaTimeModule.addSerializer(LocalDate.class,new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        javaTimeModule.addSerializer(LocalTime.class,new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
        //  时间反序列化
        javaTimeModule.addDeserializer(LocalDateTime.class,new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        javaTimeModule.addDeserializer(LocalDate.class,new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        javaTimeModule.addDeserializer(LocalTime.class,new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.registerModule(javaTimeModule);
        return objectMapper;
    }
}

二、通过@RequestParam接收

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

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

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

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
/**
 * 日期格式转换器配置
 */
@Configuration
public class DateConverterConfig {
    /**
     * LocalDateTime格式转换器
     */
    @Bean
    public Converter<String, LocalDateTime> localDateTimeConverter() {
        return new Converter<String, LocalDateTime>() {
            @Override
            public LocalDateTime convert(String timeStr) {
                return LocalDateTime.parse(timeStr,DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            }
        };
    }
    /**
     * LocalDate格式转换器
     */
    @Bean
    public Converter<String, LocalDate> localDateConverter() {
        return new Converter<String, LocalDate>() {
            @Override
            public LocalDate convert(String timeStr) {
                return LocalDate.parse(timeStr,DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            }
        };
    }
    /**
     * LocalTime格式转换器
     */
    @Bean
    public Converter<String, LocalTime> localTimeConverter() {
        return new Converter<String, LocalTime>() {
            @Override
            public LocalTime convert(String timeStr) {
                return LocalTime.parse(timeStr,DateTimeFormatter.ofPattern("HH:mm:ss"));
            }
        };
    }
}

到此这篇关于SpringBoot接收LocalDateTime参数的文章就介绍到这了,更多相关SpringBoot LocalDateTime参数内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文