一 自帶jackson處理方式
1、返回參數去除null值蛋哭,使用注解@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonInclude(JsonInclude.Include.NON_NULL)
自帶的jackson轉化的時候注益, 默認的時間格式y(tǒng)yyy-MM-dd'T'HH:mm:ss.SSS予弧,也支持
- 時間戳格式(1565003805835)匾浪,
- 2019-08-05
- 2019-08-05T19:09:450
- 2019-08-05T19:09:45+08:00
- 傳入yyyy-MM-dd HH:mm:ss這種格式刺桃,會拋出異常
19:32:10.114 [http-nio-7012-exec-7] WARN o.s.w.s.m.s.DefaultHandlerExceptionResolver - Resolved exception caused by Handler execution: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Can not deserialize value of type java.util.Date from String "2019-08-05 T 19:16:45": not a valid representation (error: Failed to parse Date value '2019-08-05 T 19:16:45': Can not parse date "2019-08-05 T 19:16:450": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSS', parsing fails (leniency? null)); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not deserialize value of type java.util.Date from String "2019-08-05 T 19:16:45": not a valid representation (error: Failed to parse Date value '2019-08-05 T 19:16:45': Can not parse date "2019-08-05 T 19:16:450": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSS', parsing fails (leniency? null))
2. 入參格式化
這時,就可以使用 Spring 的 @DateTimeFormat 注解格式化參數桶蛔,來解決上述問題。
改造 DateVo:
public class DateVo {
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
private Date date;
public void setDate(Date date){
this.date = date;
}
public Date getDate(){
return date;
}
}
再像上面一樣訪問 /date/test 漫谷,并傳入參數:2018-08-02 22:05:55仔雷,將在控制臺上打印:
date1:Thu Aug 02 22:05:55 CST 2018
date2:2018-08-02 22:05:55
可以看到舔示,加入 @DateTimeFormat 注解后參數可以被接收到了碟婆,但日期時間的格式還是需要自己再手動轉換一下。
因為 @DateTimeFormat 注解的 pattern 屬性值指定的日期時間格式并不是
將要轉換
成的日期格式惕稻,這個指定的格式是和傳入的參數對應的竖共,假如注解為:
@DateTimeFormat(pattern="yyyy/MM/dd HH:mm:ss")
則傳入的參數應該是這樣的:
2018/08/02 22:05:55
否則會拋出異常。
3. 出參格式化 (springboot默認返回格式為時間戳)
假如出參格式 "date": "1565003805835"
這個格式并不是我們想要的俺祠,那么如何將其進行格式化公给?這時就需要用到 jackson 的 @JsonFormat 注解。
改造 DateVo:
public class DateVo {
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
@JsonFormat( pattern = "yyyy-MM-dd HH:mm:ss")
private Date date;
public void setDate(Date date){
this.date = date;
}
public Date getDate(){
return date;
}
}
傳入參數:2018-08-02 22:05:55蜘渣,可以看到接口返回的結果為:
"date": "2018-08-01 14:32:57"
雖然時間格式正確了淌铐,但實際上當前時間是 “2018-08-01 22:32:57” ,早了8個小時蔫缸。因為腿准,jackson在序列化時間時是按照國際標準時間GMT進行格式化的,而在國內默認時區(qū)使用的是CST時區(qū)拾碌,兩者相差8小時吐葱。
所以街望,@JsonFormat 注解還要再加一個屬性:
@JsonFormat(
pattern = "yyyy-MM-dd HH:mm:ss",
timezone = "GMT+8"
)
二 fastjson
還原沖統(tǒng)一格式fastjson校驗弟跑,時間格式傳入可以同時兼任時間戳和日期類型
fastjson方式
public class MySpringBootApplication extends WebMvcConfigurerAdapter {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
converter.setFastJsonConfig(fastJsonConfig);
converters.add(converter);
}
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication .class, args);
}
}
參考:https://blog.csdn.net/zhou520yue520/article/details/81348926