亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

解決springboot 實(shí)體類(lèi)String轉(zhuǎn)Date類(lèi)型的坑

 更新時(shí)間:2021年10月25日 10:32:40   作者:DemonsPan  
這篇文章主要介紹了解決springboot 實(shí)體類(lèi)String轉(zhuǎn)Date類(lèi)型的坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

springboot 實(shí)體類(lèi)String轉(zhuǎn)Date類(lèi)型

前端傳入一個(gè)String的時(shí)間字符串如:2019-07-18 23:59:59

后端實(shí)體類(lèi)要在頭頂加注解:

@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")

不然會(huì)出現(xiàn)報(bào)錯(cuò)

Date解析String類(lèi)型的參數(shù)

1.首先建立String to Date 的解析實(shí)現(xiàn)

import org.apache.commons.lang3.StringUtils;
import org.springframework.core.convert.converter.Converter;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDateConverter implements Converter<String, Date> {
    private static final String dateFormat = "yyyy-MM-dd HH:mm:ss";
    private static final String shortDateFormat = "yyyy-MM-dd";
    @Override
    public Date convert(String value) {
        if (StringUtils.isEmpty(value)) {
            return null;
        }
        value = value.trim();
        try {
            if (value.contains("-")) {
                SimpleDateFormat formatter;
                if (value.contains(":")) {
                    formatter = new SimpleDateFormat(dateFormat);
                } else {
                    formatter = new SimpleDateFormat(shortDateFormat);
                }
                Date dtDate = formatter.parse(value);
                return dtDate;
            } else if (value.matches("^\\d+$")) {
                Long lDate = new Long(value);
                return new Date(lDate);
            }
        } catch (Exception e) {
            throw new RuntimeException(String.format("parser %s to Date failed", value));
        }
        throw new RuntimeException(String.format("parser %s to Date failed", value));
    }
}

2.創(chuàng)建全局的解析配置

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import javax.annotation.PostConstruct;
@Configuration
public class DateHandlerAdapter {
    @Autowired
    private RequestMappingHandlerAdapter handlerAdapter;
    /**
     * 增加字符串轉(zhuǎn)日期的全局適配器
     */
    @PostConstruct
    public void initEditableAvlidation() {
        ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) handlerAdapter
                .getWebBindingInitializer();
        if (initializer.getConversionService() != null) {
            GenericConversionService genericConversionService = (GenericConversionService) initializer
                    .getConversionService();
            genericConversionService.addConverter(new StringToDateConverter());
        }
    }
}

添加完這兩個(gè)文件以后 在傳參數(shù)類(lèi)型為Date的參數(shù)時(shí)就不會(huì)再報(bào) date解析失敗的錯(cuò)誤了。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • springboot yml配置文件定義list集合、數(shù)組和map以及使用中的錯(cuò)誤

    springboot yml配置文件定義list集合、數(shù)組和map以及使用中的錯(cuò)誤

    這篇文章主要介紹了springboot yml配置文件定義list集合、數(shù)組和map以及使用中遇到的錯(cuò)誤問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Java中線程的基本方法使用技巧

    Java中線程的基本方法使用技巧

    這篇文章主要介紹了Java中線程的基本方法使用技巧,需要的朋友可以參考下
    2017-09-09
  • GC參考手冊(cè)二java中垃圾回收原理解析

    GC參考手冊(cè)二java中垃圾回收原理解析

    由于有個(gè)垃圾回收機(jī)制,java中的額對(duì)象不在有“作用域”的概念,只有對(duì)象的引用才有“作用域”。垃圾回收可以有效的防止內(nèi)存泄露,有效的使用空閑的內(nèi)存<BR>
    2022-01-01
  • Java?C++算法題解leetcode801使序列遞增的最小交換次數(shù)

    Java?C++算法題解leetcode801使序列遞增的最小交換次數(shù)

    這篇文章主要為大家介紹了Java?C++題解leetcode801使序列遞增的最小交換次數(shù)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • 利用Kotlin + Spring Boot實(shí)現(xiàn)后端開(kāi)發(fā)

    利用Kotlin + Spring Boot實(shí)現(xiàn)后端開(kāi)發(fā)

    這篇文章主要給大家介紹了關(guān)于利用Kotlin + Spring Boot實(shí)現(xiàn)后端開(kāi)發(fā)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • Spring?Security實(shí)現(xiàn)用戶名密碼登錄詳解

    Spring?Security實(shí)現(xiàn)用戶名密碼登錄詳解

    這篇文章主要為大家詳細(xì)介紹了Spring Security如何實(shí)現(xiàn)用戶名密碼登錄功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下
    2022-10-10
  • 用Java8 stream處理數(shù)據(jù)

    用Java8 stream處理數(shù)據(jù)

    這篇文章主要介紹了用Java8 stream處理數(shù)據(jù),Java 8 API的設(shè)計(jì)者重新提出了一個(gè)新的抽象稱(chēng)為流Stream,可以讓我們以一種聲明的方式處理數(shù)據(jù),此外,數(shù)據(jù)流可以充分利用多核架構(gòu)而無(wú)需編寫(xiě)多線程的一行代碼,下面我們一起來(lái)看看文章詳細(xì)介紹
    2021-11-11
  • maven坐標(biāo)Dependencies和Exclusions的使用

    maven坐標(biāo)Dependencies和Exclusions的使用

    這篇文章主要介紹了maven坐標(biāo)Dependencies和Exclusions的使用,很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Spring Data分頁(yè)與排序的實(shí)現(xiàn)方法

    Spring Data分頁(yè)與排序的實(shí)現(xiàn)方法

    這篇文章主要給大家介紹了關(guān)于Spring Data分頁(yè)與排序的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-12-12
  • 學(xué)習(xí)非阻塞的同步機(jī)制CAS

    學(xué)習(xí)非阻塞的同步機(jī)制CAS

    現(xiàn)代的處理器都包含對(duì)并發(fā)的支持,其中最通用的方法就是比較并交換(compare and swap),簡(jiǎn)稱(chēng)CAS。下面我們來(lái)一起學(xué)習(xí)一下吧
    2019-05-05

最新評(píng)論