最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

前言

最近在后臺(tái)開(kāi)發(fā)中遇到了時(shí)間參數(shù)的坑,就單獨(dú)把這個(gè)問(wèn)題提出來(lái)找時(shí)間整理了一下;

正文

測(cè)試方法

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ù)的幾種方式(實(shí)驗(yàn))

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

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

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

解決方案

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

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

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

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

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

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

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

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

二 接收所有時(shí)間格式

  • yyyy-MM-dd HH:mm:ss 格式
  • yyyy-MM-dd 格式
  • 時(shí)間戳
  • yyyy/MM/dd HH:mm:ss 格式
  • yyyy/MM/dd 格式

注意

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

工具類(lèi):

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: 時(shí)間轉(zhuǎn)換類(lèi)
 */
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)) {
        //時(shí)間戳
        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));
  }
}

將時(shí)間轉(zhuǎn)換類(lèi)應(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í)間參數(shù)這個(gè)坑還是有點(diǎn)大的,之前都是針對(duì)性的處理,只要一變化就沒(méi)法了;現(xiàn)在這個(gè)還是可以應(yīng)付基本上會(huì)出現(xiàn)的錯(cuò)誤了;

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

相關(guān)文章

  • Java實(shí)現(xiàn)將Markdown轉(zhuǎn)換為純文本

    Java實(shí)現(xiàn)將Markdown轉(zhuǎn)換為純文本

    這篇文章主要為大家詳細(xì)介紹了兩種在 Java 中實(shí)現(xiàn) Markdown 轉(zhuǎn)純文本的主流方法,文中的示例代碼講解詳細(xì),大家可以根據(jù)需求選擇適合的方案
    2025-03-03
  • Java異常類(lèi)型及處理詳情

    Java異常類(lèi)型及處理詳情

    這篇文章主要介紹了Java異常類(lèi)型及處理, 異常指的是程序在執(zhí)行過(guò)程中,出現(xiàn)了非正常情況,導(dǎo)致了java的jvm停止。感興趣的小伙伴就和小編一起來(lái)學(xué)習(xí)下面文章的具體內(nèi)容吧
    2021-09-09
  • 從零開(kāi)始學(xué)JAVA之可變參數(shù)

    從零開(kāi)始學(xué)JAVA之可變參數(shù)

    本文是從零開(kāi)始學(xué)JAVA的第一篇,屬于Java基礎(chǔ)知識(shí)介紹的第一部分,主要介紹Java的可變參數(shù),非常使用,希望對(duì)大家有所幫助
    2014-10-10
  • Java面向?qū)ο缶幊蹋ǚ庋b/繼承/多態(tài))實(shí)例解析

    Java面向?qū)ο缶幊蹋ǚ庋b/繼承/多態(tài))實(shí)例解析

    這篇文章主要介紹了Java面向?qū)ο缶幊蹋ǚ庋b/繼承/多態(tài))實(shí)例解析的相關(guān)內(nèi)容,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-10-10
  • 詳解tryAcquire()、addWaiter()、acquireQueued()

    詳解tryAcquire()、addWaiter()、acquireQueued()

    這篇文章主要tryAcquire()、addWaiter()、acquireQueued()的用法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Spring AOP使用@Aspect注解 面向切面實(shí)現(xiàn)日志橫切的操作

    Spring AOP使用@Aspect注解 面向切面實(shí)現(xiàn)日志橫切的操作

    這篇文章主要介紹了Spring AOP使用@Aspect注解 面向切面實(shí)現(xiàn)日志橫切的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • SpringBoot+Spring Security+JWT實(shí)現(xiàn)RESTful Api權(quán)限控制的方法

    SpringBoot+Spring Security+JWT實(shí)現(xiàn)RESTful Api權(quán)限控制的方法

    這篇文章主要介紹了SpringBoot+Spring Security+JWT實(shí)現(xiàn)RESTful Api權(quán)限控制的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • ActiveMQ簡(jiǎn)單入門(mén)(新手必看篇)

    ActiveMQ簡(jiǎn)單入門(mén)(新手必看篇)

    下面小編就為大家?guī)?lái)一篇ActiveMQ簡(jiǎn)單入門(mén)(新手必看篇)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-06-06
  • Java中 ? extends T 和 ? super T的理解

    Java中 ? extends T 和 ? super&nb

    本文主要介紹了Java中 ? extends T 和 ? super T的理解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • lombok注解介紹小結(jié)

    lombok注解介紹小結(jié)

    lombok是一個(gè)可以幫助我們簡(jiǎn)化java代碼編寫(xiě)的工具類(lèi),這篇文章主要介紹了lombok注解介紹小結(jié),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-11-11

最新評(píng)論

夏津县| 石渠县| 红桥区| 满城县| 江源县| 洞头县| 龙江县| 大化| 柞水县| 社会| 威远县| 瓦房店市| 澄城县| 广平县| 峨边| 荥阳市| 方正县| 蒲江县| 珲春市| 工布江达县| 洞口县| 扎鲁特旗| 清远市| 兴海县| 诸城市| 岳阳市| 东莞市| 惠州市| 中宁县| 开远市| 丹寨县| 渭源县| 天长市| 德格县| 大石桥市| 永德县| 钟山县| 扬州市| 博白县| 松阳县| 金川县|