2,3、跟自定义Formatter的操作一样
1、实现这个自定义的转换器类
自定义一个converter
/**
* A converter converts a source object of type {@code String} to a target of type {@code Date}.
*/
public class MyConverter implements Converter<String,Date>{
@Override
public Date convert(String s) {
try {
return DateUtil.S_DAY_DATEFORMAT.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}配置自定义converter
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
// @Bean
// public Formatter<Date> myDateFormatter(){
// return new MyDateFormatter();
// }
//
// @Override
// public void addFormatters(FormatterRegistry registry) {
// registry.addFormatter(myDateFormatter());
// super.addFormatters(registry);
// }
@Bean
public Converter myConverter(){
return new MyConverter();
}
}使用
/**
*date:2018-05-24
* @param date
*/
@PostMapping("date")
public void dateType(Date date){
System.out.println(date);
}
1、同Formatter类似,Converter自定义时实现Converter接口,指定泛型<S, T>,即源对象和目标对象,然后实现convert方法;
2、同样在xml中进行配置,将自定义转换器注入FormattingConversionServiceFactoryBean,再将该bean作为mvc的conversion-service
3、不同在于,Formatter只能将String作为源对象,而Converter则可以自行定义
1、同Formatter类似,Converter自定义时实现Converter接口,指定泛型<S, T>,即源对象和目标对象,然后实现convert方法;
2、同样在xml中进行配置,将自定义转换器注入FormattingConversionServiceFactoryBean,再将该bean作为mvc的conversion-service
3、不同在于,Formatter只能将String作为源对象,而Converter则可以自行定义