時間格式註解:@Temporal(TemporalType.XXXX)時間格式化hibernate jpa註解
時間格式註解:@Temporal(TemporalType.XXXX)時間格式化hibernate jpa註解
TemporalType取值包括:DATE/TIME/TIMESTRAMP(分別表示:日期、時間、日期+時間)
eg:
@Temporal(TemporalType.DATE)
@Column(name = "applyDate", nullable = false, length = 10) //顯示: 2011-04-12
@Temporal(TemporalType.TIME)
@Column(name = "applyDate") //顯示: 22:50:30
@Temporal(TemporalType. TIMESTRAMP)
@Column(name = "applyDate") //顯示: 2011-04-12 22:51:34.0
雖然定義了格式,但是發現前臺獲取的日期還是一串數字,SpringBoot可以使用@JsonSerialize註解
@JsonSerialize(using=CustomDateTimeSerializer.class)
public Date getTime() {
return time;
}
然後去實現這個類即可,在使用getTime,會自動去轉換格式
public class CustomDateTimeSerializer extends JsonSerializer<Date>{
@Override
public void serialize(Date value, JsonGenerator gen, SerializerProvider serializers)
throws IOException, JsonProcessingException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
gen.writeString(sdf.format(value));
}
}
上述將成功將日期傳回前臺後,我們發現若前臺想傳日期回到controller也會出現格式轉換問題,此時可以使用@InitBinder
@InitBinder
用於在@Controller
中標註於方法上,表示爲當前控制器註冊一個屬性編輯器,只對當前的Controller有效。@InitBinder
標註的方法必須有一個參數WebDataBinder
。所謂的屬性編輯器可以理解就是幫助我們完成參數綁定。
@ResponseBody
@RequestMapping(value = "/test")
public String test(@RequestParam String name,@RequestParam Date date) throws Exception {
System.out.println(name);
System.out.println(date);
return name;
}
@InitBinder
public void initBinder(WebDataBinder binder){
binder.registerCustomEditor(String.class,
new StringTrimmerEditor(true));
binder.registerCustomEditor(Date.class,
new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false));
}
上面例子中,@InitBinder
方法會幫助我們把String類型的參數先trim再綁定,而對於Date類型的參數會先格式化在綁定。例如當請求是/test?name=%20zero%20&date=2018-05-22
時,會把zero綁定到name,再把時間串格式化爲Date類型,再綁定到date。
這裏的@InitBinder
方法只對當前Controller生效,要想全局生效,可以使用@ControllerAdvice
。通過@ControllerAdvice
可以將對於控制器的全局配置放置在同一個位置,註解了@ControllerAdvice
的類的方法可以使用@ExceptionHandler
,@InitBinder
,@ModelAttribute
註解到方法上,這對所有註解了@RequestMapping
的控制器內的方法有效。
@ControllerAdvice
public class GlobalControllerAdvice {
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(String.class,
new StringTrimmerEditor(true));
binder.registerCustomEditor(Date.class,
new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false));
}
}
除了使用@ControllerAdvice
來配置全局的WebDataBinder
,還可以使用RequestMappingHandlerAdapter
:
@Bean
public RequestMappingHandlerAdapter webBindingInitializer() {
RequestMappingHandlerAdapter adapter = new RequestMappingHandlerAdapter();
adapter.setWebBindingInitializer(new WebBindingInitializer(){
@Override
public void initBinder(WebDataBinder binder, WebRequest request) {
binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false));
}
});
return adapter;
}
如上示例,可以實現同樣的效果。
@ControllerAdvice
中除了配置@InitBinder
,還可以有@ExceptionHandler
用於全局處理控制器裏面的異常;@ModelAttribute
作用是綁定鍵值對到Model裏,讓全局的@RequestMapping
都能獲得在此處設置的鍵值對。
補充:如果 @ExceptionHandler
註解中未聲明要處理的異常類型,則默認爲方法參數列表中的異常類型。示例:
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
@ResponseBody
String handleException(Exception e){
return "Exception Deal! " + e.getMessage();
}
}