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

基于springboot處理date參數(shù)過程解析

 更新時間:2019年12月05日 09:34:20   作者:guodaye  
這篇文章主要介紹了基于springboot處理date參數(shù)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

這篇文章主要介紹了基于springboot處理date參數(shù)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

前言

最近在后臺開發(fā)中遇到了時間參數(shù)的坑,就單獨把這個問題提出來找時間整理了一下;

正文

測試方法

bean代碼:

public class DateModelNoAnnotation {
  private Integer id;
  private Date receiveDate;
}

controller代碼:

@RestController
@RequestMapping("/date")
public class DateVerifyController {
  //  方式一
  @PostMapping("/no")
  public String dateUnNoAnnotation(DateModelNoAnnotation dateModelNoAnnotation){
    System.out.println(dateModelNoAnnotation.toString());
    return "SUCCESS";
  }

//  方式二
  @PostMapping("/has")
  public String dateHasAnnotation(@RequestBody DateModelNoAnnotation dateModelNoAnnotation){
    System.out.println(dateModelNoAnnotation.toString());
    return "SUCCESS";
  }
//  方式三
  @GetMapping("/param")
  public String dateParams(@RequestParam("id")Integer id, @RequestParam("receiveDate")Date receiveDate){
    System.out.println("id====="+id);
    System.out.println("receiveDate====="+receiveDate);
    System.out.println("receiveDate====="+receiveDate.getTime());
    return "SUCCESS";
  }
//  方式四
  @GetMapping("/no/param")
  public String dateNoParams(Integer id,Date receiveDate){
    System.out.println("id====="+id);
    System.out.println("receiveDate====="+receiveDate);
    System.out.println("receiveDate====="+receiveDate.getTime());
    return "SUCCESS";
  }
}

接收參數(shù)的幾種方式(實驗)

  • 通過bean來接收數(shù)據(jù)(表單方式)
    • 這種方式只支持"yyyy/MM/dd HH:mm:ss"這種格式的time參數(shù)
  • 通過bean來接收數(shù)據(jù)(json格式)
    • 這種方式只支持"yyyy-MM-dd HH:mm:ss"這種格式的time參數(shù)
  • 通過RequestParam注解
    • 這種方式只支持"yyyy/MM/dd HH:mm:ss"這種格式的time參數(shù)
  • 不通過RequestParam注解
    • 這種方式只支持"yyyy/MM/dd HH:mm:ss"這種格式的time參數(shù)

以上幾種接收參數(shù)的方式接收的參數(shù)格式并不統(tǒng)一,而且有時候web前端傳入的時間參數(shù)為時間戳,還得寫修改接口或者讓其自己修改格式;

后端給前端統(tǒng)一返回json格式的數(shù)據(jù),且時間格式為"yyyy-MM-dd HH:mm:ss"

解決方案

開發(fā)之前統(tǒng)一時間接口接收的時間格式

一 yyyy/MM/dd HH:mm:ss 格式

后端所有接口統(tǒng)一接收"yyyy/MM/dd HH:mm:ss"或"yyyy/MM/dd"格式時間參數(shù)

第一種: 舍棄上邊的方式二的接口

第二種:不舍棄方拾二,在bean的時間屬性上添加JsonFormat注解,例如:

  com.fasterxml.jackson.annotation.JsonFormat;
   @JsonFormat(timezone = "GMT+8",pattern = "yyyy/MM/dd HH:mm:ss")
  private Date receiveDate;

優(yōu)勢: 不舍棄方式二接口,且統(tǒng)一了時間格式

使用該注解的弊端: 當(dāng)pattern="yyyy/MM/dd" 時, 只支持處理“2019/09/03"格式時間參數(shù),不支持“2019/09/03 00:00:00”,且會報錯,當(dāng)pattern="yyyy/MM/dd HH:mm:ss"時,只支持處理“2019/09/03 00:00:00"格式時間參數(shù),其余格式均會報錯;

二 接收所有時間格式

  • yyyy-MM-dd HH:mm:ss 格式
  • yyyy-MM-dd 格式
  • 時間戳
  • yyyy/MM/dd HH:mm:ss 格式
  • yyyy/MM/dd 格式

注意

該方式不對json或xml的數(shù)據(jù)處理,比如使用@RequestBody注解的bean(也就是方式二)

工具類:

import org.springframework.core.convert.converter.Converter;
import org.springframework.util.StringUtils;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * @author gyc
 * @title: DateConverter
 * @projectName app
 * @date 2019/8/1914:36
 * @description: 時間轉(zhuǎn)換類
 */
public class CourseDateConverter implements Converter<String, Date> {
  private static final String dateFormat = "yyyy-MM-dd HH:mm:ss";
  private static final String dateFormata = "yyyy-MM-dd HH:mm:ss";
  private static final String shortDateFormat = "yyyy-MM-dd";
  private static final String shortDateFormata = "yyyy/MM/dd";
  private static final String timeStampFormat = "^\\d+$";
  @Override
  public Date convert(String value) {
    if(StringUtils.isEmpty(value)) {
      return null;
    }
    value = value.trim();
    try {
      if (value.contains("-")) {
        SimpleDateFormat formatter;
        if (value.contains(":")) {
          //yyyy-MM-dd HH:mm:ss 格式
          formatter = new SimpleDateFormat(dateFormat);
        } else {
          //yyyy-MM-dd 格式
          formatter = new SimpleDateFormat(shortDateFormat);
        }
        return formatter.parse(value);
      } else if (value.matches(timeStampFormat)) {
        //時間戳
        Long lDate = new Long(value);
        return new Date(lDate);
      }else if (value.contains("/")){
        SimpleDateFormat formatter;
        if (value.contains(":")) {
//          yyyy/MM/dd HH:mm:ss 格式
          formatter = new SimpleDateFormat(dateFormata);
        } else {
//          yyyy/MM/dd 格式
          formatter = new SimpleDateFormat(shortDateFormata);
        }
        return formatter.parse(value);
      }
    } catch (Exception e) {
      throw new RuntimeException(String.format("parser %s to Date fail", value));
    }
    throw new RuntimeException(String.format("parser %s to Date fail", value));
  }
}

將時間轉(zhuǎn)換類應(yīng)用到接口上

介紹兩種方式:使用@Component + @PostConstruct或@ControllerAdvice + @InitBinder

第一種方式:

@Component + @PostConstruct

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import javax.annotation.PostConstruct;

@Component
public class WebConfigBeans {
 @Autowired
 private RequestMappingHandlerAdapter handlerAdapter;
 @PostConstruct
 public void initEditableAvlidation() {
  ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer)handlerAdapter.getWebBindingInitializer();
  if(initializer.getConversionService()!=null) {
   GenericConversionService genericConversionService = (GenericConversionService)initializer.getConversionService();

   genericConversionService.addConverter(new DateConverterConfig());

  }
 }
}

第二種方式:

@ControllerAdvice + @InitBinder

import com.aegis.config.converter.DateConverter;
import com.aegis.model.bean.common.JsonResult;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
@ControllerAdvice
public class CourseControllerHandler {
  @InitBinder
  public void initBinder(WebDataBinder binder) {
    GenericConversionService genericConversionService = (GenericConversionService) binder.getConversionService();
    if (genericConversionService != null) {
      genericConversionService.addConverter(new CourseDateConverter());
    }
  }
}

最后

我使用的最后的一種方法的第二種方式

總結(jié)

時間參數(shù)這個坑還是有點大的,之前都是針對性的處理,只要一變化就沒法了;現(xiàn)在這個還是可以應(yīng)付基本上會出現(xiàn)的錯誤了;

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java之理解Redis回收算法LRU案例講解

    Java之理解Redis回收算法LRU案例講解

    這篇文章主要介紹了Java之理解Redis回收算法LRU案例講解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • Spring MVC多種情況下進行文件上傳的實例

    Spring MVC多種情況下進行文件上傳的實例

    上傳是Web工程中很常見的功能,SpringMVC框架簡化了文件上傳的代碼,本文給大家總結(jié)了Spring MVC多種情況下進行文件上傳的實例,并通過代碼示例給大家介紹的非常詳細,需要的朋友可以參考下
    2024-02-02
  • Java使用HashMap實現(xiàn)并查集

    Java使用HashMap實現(xiàn)并查集

    這篇文章主要為大家詳細介紹了Java使用HashMap實現(xiàn)并查集,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • SpringSecurity 自定義認證登錄的項目實踐

    SpringSecurity 自定義認證登錄的項目實踐

    本文主要介紹了SpringSecurity 自定義認證登錄的項目實踐,以手機驗證碼登錄為例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-08-08
  • Java中BigInteger與BigDecimal類用法總結(jié)

    Java中BigInteger與BigDecimal類用法總結(jié)

    在Java中有兩個用于大數(shù)字運算的類,分別是java.math.BigInteger類 和 java.math.BigDecimal類,這兩個類都可以用于高精度計算,BigInteger類是針對整型大數(shù)字的處理類,而BigDecimal類是針對大小數(shù)的處理類,接下來帶大家來學(xué)習(xí)一下,在Java中如何處理大數(shù)字
    2023-05-05
  • 詳解Mybatis模板(已優(yōu)化)適合小白

    詳解Mybatis模板(已優(yōu)化)適合小白

    這篇文章主要介紹了Mybatis模板(已優(yōu)化)適合小白,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03
  • Java實現(xiàn)新建有返回值的線程的示例詳解

    Java實現(xiàn)新建有返回值的線程的示例詳解

    本文主要介紹了一個Java多線程的例題,題目是:使用ThreadLocal管理一號和二號線程,分別存入100元,在三號線程中使用利用一號和二號的計算結(jié)果來算出賬戶的實際金額。感興趣的可以了解一下
    2022-09-09
  • Spring在SingleTon模式下的線程安全詳解

    Spring在SingleTon模式下的線程安全詳解

    這篇文章主要介紹了Spring在SingleTon模式下的線程安全詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Idea中maven項目實現(xiàn)登錄驗證碼功能

    Idea中maven項目實現(xiàn)登錄驗證碼功能

    這篇文章主要介紹了Idea中maven項目實現(xiàn)登錄驗證碼功能,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-12-12
  • Java虛擬機最多支持多少個線程的探討

    Java虛擬機最多支持多少個線程的探討

    這篇文章主要介紹了Java虛擬機最多支持多少個線程的問題,從StackOverflow上摘錄而來,需要的朋友可以參考下
    2014-04-04

最新評論