spring boot 配置全局日期类型转换器
2021-06-29 08:05
标签:add rri edit exce void ice boot convert extends import org.springframework.core.convert.converter.Converter; import java.text.ParseException; @Component 2.在配置类中注册自定义类型转换器 import com.example.demo.convert.MyDataConvert; import javax.annotation.PostConstruct; @Configuration } spring boot 配置全局日期类型转换器 标签:add rri edit exce void ice boot convert extends 原文地址:http://blog.51cto.com/13963665/2175277
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MyDataConvert implements Converter
//这里可以自己灵活变通
private String [] pattern = {"yyyy-MM-dd","yyyy年MM月dd日"};@Override
br/>@Override
public Date convert(String s) {
System.out.println("convert");
for (int i=0;i
return new SimpleDateFormat(pattern[i]).parse(s);
} catch (ParseException e) {
continue;
}
}
return null;
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
public class WebConfigurer extends WebMvcConfigurationSupport {@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 MyDataConvert());//添加自定义的类型转换器
}
}
上一篇:Java 1.8 Unsafe
下一篇:钉钉扫码登录 java
文章标题:spring boot 配置全局日期类型转换器
文章链接:http://soscw.com/index.php/essay/99281.html