spring boot 配置全局日期类型转换器
2021-06-29 08:06
标签:public ada mapping tor bin eth col util code 1. 首先自定义一个类型转换器 2.在配置类中注册自定义类型转换器 spring boot 配置全局日期类型转换器 标签:public ada mapping tor bin eth col util code 原文地址:https://www.cnblogs.com/sjxxx/p/9646556.htmlimport org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
@Component
public class MyDataConvert implements Converter
import com.example.demo.convert.MyDataConvert;
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;
import javax.annotation.PostConstruct;
@Configuration
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());//添加自定义的类型转换器
}
}
}