基于SpringBoot項目遇到的坑--Date入?yún)栴}
SpringBoot Date入?yún)栴}
springboot項目遇到的坑-----使用@ResponseBody @RequestBody,對象Date 類型入?yún)?,返回json格式化
1.傳輸中的Date類型時間不準確
時區(qū)會有8個小時偏差
原因分析
而SpringBoot默認的是Jackson框架轉換,而Jackson默認的時間時區(qū)是GMT,對于中國時間少8個小時
解決方案
在傳輸?shù)腄ate屬性字段上加此注解
@JsonFormat(timezone = “GMT+8”,pattern = “yyyy-MM-dd”)
在傳輸實體類中定義一個Long型成員變量存儲時間戳 傳輸過程中只傳時間戳 后臺將其進行轉換為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.后臺返回的json數(shù)據(jù)
其中Date類型接收會自動轉換成Long類型的時間戳

原因分析:
springboot1.x版本默認的json處理是jackson 會將date字段返回時間戳
解決方案:
全局配置
spring: jackson: time-zone: GMT+8 date-format: yyyy-MM-dd HH:mm:ss
如果個別實體需要使用其他格式的 pattern,在實體上加入注解即可
@JsonFormat(timezone = “GMT+8”,pattern = “yyyy-MM-dd”) private Date time;

springboot接口入?yún)⒌囊恍﹩栴}
最近在工作中遇到一個接口入?yún)㈩愋娃D換錯誤未被處理的問題,于是整理了一些關于springmvc入?yún)⒌膯栴}
入?yún)⒔壎?/h3>
1、入?yún)⒅形覀冏畛R姷氖莇ate類型的參數(shù)轉換,這個可以通過注解來實現(xiàn)參數(shù)類型的轉換,只需在bean對象的屬性上方添加注解@DateTimeFormat(pattern=“yyyy-MM-dd”),pattern為時間對象的格式化

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ù)類型轉換器
首先建立一個實現(xiàn)Converter的轉換器
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ù)轉換器綁定到springmvc的配置中
@Configuration
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 StringToDateConverter());
}
}
}
入?yún)㈠e誤全局異常處理
在springmvc的模型中,若參數(shù)轉換出現(xiàn)異常,會直接跳轉到默認的錯誤400頁面,如果我們做的為接口,需返回一個代表錯誤的json對象時,我們可以使用一個全局的異常處理類,類上添加注解@RestControllerAdvice使得異常處理后返回rest風格的對象,使用@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ù)類型錯誤");
return resp;
}
}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
淺談Java循環(huán)中的For和For-each哪個更快
本文主要介紹了淺談Java循環(huán)中的For和For-each哪個更快,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-08-08
Spring整合Quartz實現(xiàn)定時任務調度的方法
下面小編就為大家?guī)硪黄猄pring整合Quartz實現(xiàn)定時任務調度的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11

