全局处理异常数据映射
@Configuration
public class DataConverConfig {
@Bean
public Converter< String, Date> stringDateConvert ( ) {
return new Converter < String, Date> ( ) {
@Override
public Date convert ( String source) {
if ( StringUtils. isEmpty ( source) || "null" . equals ( source) ) {
return null ;
}
SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) ;
Date date = null ;
try {
date = sdf. parse ( ( String) source) ;
} catch ( Exception e) {
SimpleDateFormat sdfday = new SimpleDateFormat ( "yyyy-MM-dd" ) ;
try {
date = sdfday. parse ( ( String) source) ;
} catch ( ParseException e1) {
try {
source = source. replace ( "GMT" , "" ) . replaceAll ( "\\(.*\\)" , "" ) ;
SimpleDateFormat format = new SimpleDateFormat ( "EEE MMM dd yyyy HH:mm:ss z" , Locale. ENGLISH ) ;
date = format. parse ( source) ;
return date;
} catch ( Exception exe) {
return null ;
}
}
}
return date;
}
} ;
}
@Bean
public Converter< String, Long> stringLongConvert ( ) {
return new Converter < String, Long> ( ) {
@Override
public Long convert ( String source) {
try {
if ( "null" . equals ( source) ) {
return null ;
}
} catch ( Exception e1) {
e1. printStackTrace ( ) ;
return null ;
}
return Long. valueOf ( source) ;
}
} ;
}
}