基于SpringBoot項(xiàng)目遇到的坑--Date入?yún)栴}
SpringBoot Date入?yún)栴}
springboot項(xiàng)目遇到的坑-----使用@ResponseBody @RequestBody,對(duì)象Date 類型入?yún)?,返回json格式化
1.傳輸中的Date類型時(shí)間不準(zhǔn)確
時(shí)區(qū)會(huì)有8個(gè)小時(shí)偏差
原因分析
而SpringBoot默認(rèn)的是Jackson框架轉(zhuǎn)換,而Jackson默認(rèn)的時(shí)間時(shí)區(qū)是GMT,對(duì)于中國時(shí)間少8個(gè)小時(shí)
解決方案
在傳輸?shù)腄ate屬性字段上加此注解
@JsonFormat(timezone = “GMT+8”,pattern = “yyyy-MM-dd”)
在傳輸實(shí)體類中定義一個(gè)Long型成員變量存儲(chǔ)時(shí)間戳 傳輸過程中只傳時(shí)間戳 后臺(tái)將其進(jìn)行轉(zhuǎn)換為Date然后賦值
class Test{ private Date time; private Long timeLong; } @PostMapping("/test") public Test test(@RequestBody Test test){ test.setTime(new Date(test.getTimeLone())); return test; }
2.后臺(tái)返回的json數(shù)據(jù)
其中Date類型接收會(huì)自動(dòng)轉(zhuǎn)換成Long類型的時(shí)間戳
原因分析:
springboot1.x版本默認(rèn)的json處理是jackson 會(huì)將date字段返回時(shí)間戳
解決方案:
全局配置
spring: jackson: time-zone: GMT+8 date-format: yyyy-MM-dd HH:mm:ss
如果個(gè)別實(shí)體需要使用其他格式的 pattern,在實(shí)體上加入注解即可
@JsonFormat(timezone = “GMT+8”,pattern = “yyyy-MM-dd”) private Date time;
springboot接口入?yún)⒌囊恍﹩栴}
最近在工作中遇到一個(gè)接口入?yún)㈩愋娃D(zhuǎn)換錯(cuò)誤未被處理的問題,于是整理了一些關(guān)于springmvc入?yún)⒌膯栴}
入?yún)⒔壎?/h3>
1、入?yún)⒅形覀冏畛R姷氖莇ate類型的參數(shù)轉(zhuǎn)換,這個(gè)可以通過注解來實(shí)現(xiàn)參數(shù)類型的轉(zhuǎn)換,只需在bean對(duì)象的屬性上方添加注解@DateTimeFormat(pattern=“yyyy-MM-dd”),pattern為時(shí)間對(duì)象的格式化
2、在controller類里定義數(shù)據(jù)綁定類
/** * 在controller層中加入一段數(shù)據(jù)綁定代碼 * @param webDataBinder */ @InitBinder public void initBinder(WebDataBinder webDataBinder) throws Exception{ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); simpleDateFormat.setLenient(false); webDataBinder.registerCustomEditor(Date.class , new CustomDateEditor(simpleDateFormat , true)); }
3、定義全局的參數(shù)類型轉(zhuǎn)換器
首先建立一個(gè)實(shí)現(xiàn)Converter的轉(zhuǎn)換器
public class DateConverter implements Converter<String,Date> { private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Override public Date convert(String s) { if ("".equals(s) || s == null) { return null; } try { return simpleDateFormat.parse(s); } catch (ParseException e) { e.printStackTrace(); } return null; } }
然后將該參數(shù)轉(zhuǎn)換器綁定到springmvc的配置中
@Configuration public class WebConfigBeans { @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()); } } }
入?yún)㈠e(cuò)誤全局異常處理
在springmvc的模型中,若參數(shù)轉(zhuǎn)換出現(xiàn)異常,會(huì)直接跳轉(zhuǎn)到默認(rèn)的錯(cuò)誤400頁面,如果我們做的為接口,需返回一個(gè)代表錯(cuò)誤的json對(duì)象時(shí),我們可以使用一個(gè)全局的異常處理類,類上添加注解@RestControllerAdvice使得異常處理后返回rest風(fēng)格的對(duì)象,使用@ControllerAdvice返回頁面
@RestControllerAdvice public class ControllerAdvice { @ExceptionHandler(value = {org.springframework.validation.BindException.class}) public BaseResp dealDateFarmatException(Throwable exception) { BaseResp resp = new BaseResp(); resp.setCode("400"); resp.setStatus(false); resp.setMsg("參數(shù)類型錯(cuò)誤"); return resp; } }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
淺談Java循環(huán)中的For和For-each哪個(gè)更快
本文主要介紹了淺談Java循環(huán)中的For和For-each哪個(gè)更快,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08Spring整合Quartz實(shí)現(xiàn)定時(shí)任務(wù)調(diào)度的方法
下面小編就為大家?guī)硪黄猄pring整合Quartz實(shí)現(xiàn)定時(shí)任務(wù)調(diào)度的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11Java數(shù)據(jù)結(jié)構(gòu)超詳細(xì)分析二叉搜索樹
二叉搜索樹是以一棵二叉樹來組織的。每個(gè)節(jié)點(diǎn)是一個(gè)對(duì)象,包含的屬性有l(wèi)eft,right,p和key,其中,left指向該節(jié)點(diǎn)的左孩子,right指向該節(jié)點(diǎn)的右孩子,p指向該節(jié)點(diǎn)的父節(jié)點(diǎn),key是它的值2022-03-03