SpringBoot Date入參問題
springboot項目遇到的坑-----使用@ResponseBody @RequestBody,對象Date 類型入參,返回json格式化
1.傳輸中的Date類型時間不準確
時區會有8個小時偏差
原因分析
而SpringBoot默認的是Jackson框架轉換,而Jackson默認的時間時區是GMT,對于中國時間少8個小時
解決方案
在傳輸的Date屬性字段上加此注解
1
|
@JsonFormat (timezone = “GMT+ 8 ”,pattern = “yyyy-MM-dd”) |
在傳輸實體類中定義一個Long型成員變量存儲時間戳 傳輸過程中只傳時間戳 后臺將其進行轉換為Date然后賦值
1
2
3
4
5
6
7
8
9
10
|
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數據
其中Date類型接收會自動轉換成Long類型的時間戳
原因分析:
springboot1.x版本默認的json處理是jackson 會將date字段返回時間戳
解決方案:
全局配置
1
2
3
4
|
spring: jackson: time-zone: GMT+ 8 date-format: yyyy-MM-dd HH:mm:ss |
如果個別實體需要使用其他格式的 pattern,在實體上加入注解即可
1
2
|
@JsonFormat (timezone = “GMT+ 8 ”,pattern = “yyyy-MM-dd”) private Date time; |
springboot接口入參的一些問題
最近在工作中遇到一個接口入參類型轉換錯誤未被處理的問題,于是整理了一些關于springmvc入參的問題
入參綁定
1、入參中我們最常見的是date類型的參數轉換,這個可以通過注解來實現參數類型的轉換,只需在bean對象的屬性上方添加注解@DateTimeFormat(pattern=“yyyy-MM-dd”),pattern為時間對象的格式化
2、在controller類里定義數據綁定類
1
2
3
4
5
6
7
8
9
10
|
/** * 在controller層中加入一段數據綁定代碼 * @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、定義全局的參數類型轉換器
首先建立一個實現Converter的轉換器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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 ; } } |
然后將該參數轉換器綁定到springmvc的配置中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@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()); } } } |
入參錯誤全局異常處理
在springmvc的模型中,若參數轉換出現異常,會直接跳轉到默認的錯誤400頁面,如果我們做的為接口,需返回一個代表錯誤的json對象時,我們可以使用一個全局的異常處理類,類上添加注解@RestControllerAdvice使得異常處理后返回rest風格的對象,使用@ControllerAdvice返回頁面
1
2
3
4
5
6
7
8
9
10
11
|
@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( "參數類型錯誤" ); return resp; } } |
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/weixin_42413097/article/details/95732864