此自定义属性编辑器用于将String转换为Date类型,具体代码如下:
目录结构
DateEditor
package com.xiayc.dateeditor.editor;
import org.springframework.util.StringUtils;
import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by xyc on 2017/8/5 0005.
*/
public class DateEditor extends PropertyEditorSupport {
/**
* 将外部设置的字符串转换为内部属性的值
*
* @param text The string to be parsed.
*/
@Override
public void setAsText(String text) throws IllegalArgumentException {
//判断是否为空
if (text == null || text.trim().equals("")) {
super.setValue(null);
return;
}
//判断是否为时间戳类型
text = text.trim();
if (text.matches("^[0-9]*$")) {
super.setValue(new Date(Long.parseLong(text)));
return;
}
//选取日期格式化格式(目前只支持:yyyy-MM-dd、HH:mm:ss、yyyy-MM-dd HH:mm:ss)
String datePattern = null;
if (StringUtils.countOccurrencesOf(text, "-") == 2) {
datePattern = "yyyy-MM-dd";
}
if (StringUtils.countOccurrencesOf(text, ":") == 2) {
datePattern = datePattern == null ? "HH:mm:ss" : datePattern + " HH:mm:ss";
}
if (datePattern == null) {
throw new IllegalAccessError("不支持的时间格式,支持的时间格式:yyyy-MM-dd、HH:mm:ss、yyyy-MM-dd HH:mm:ss");
}
//进行格式化
SimpleDateFormat format = new SimpleDateFormat(datePattern);
try {
super.setValue(format.parse(text));
} catch (ParseException e) {
throw new IllegalAccessError("格式化时间格式异常:" + e.getMessage());
}
}
}
- DefaultWebBindingInitializer
package com.xiayc.dateeditor.initializer;
import com.xiayc.dateeditor.editor.DateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.WebBindingInitializer;
import org.springframework.web.context.request.WebRequest;
import java.util.Date;
/**
* Created by xyc on 2017/8/5 0005.
*/
public class DefaultWebBindingInitializer implements WebBindingInitializer {
@Override
public void initBinder(WebDataBinder webDataBinder, WebRequest webRequest) {
/**
* 注册Date类型属性编辑器
*/
webDataBinder.registerCustomEditor(Date.class, new DateEditor());
}
}
- DefaultBeanPostProcessor
package com.xiayc.dateeditor.processor;
import com.xiayc.dateeditor.initializer.DefaultWebBindingInitializer;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
/**
* Created by xyc on 2017/8/5 0005.
*/
@Component //@Component注解用于启用此功能
public class DefaultBeanPostProcessor implements BeanPostProcessor {
/**
* 实例化、依赖注入完毕,在调用显示的初始化之前完成一些定制的初始化任务
*
* @param o
* @param s
* @return
* @throws BeansException
*/
@Override
public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
if (o instanceof RequestMappingHandlerAdapter) {
RequestMappingHandlerAdapter adapter = (RequestMappingHandlerAdapter) o;
adapter.setWebBindingInitializer(new DefaultWebBindingInitializer());
}
return o;
}
/**
* 实例化、依赖注入、初始化完毕时执行
*
* @param o
* @param s
* @return
* @throws BeansException
*/
@Override
public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
return o;
}
}
- DateEditorController
package com.xiayc.dateeditor.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
/**
* Created by xyc on 2017/8/5 0005.
*/
@RestController
@RequestMapping("/dateEditor")
public class DateEditorController {
/**
* 进行测试
*
* @param date
* @return
*/
@GetMapping("/test")
public Date test(Date date) {
System.out.println(date);
return date;
}
}
- DateEditorApplication
package com.xiayc.dateeditor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DateEditorApplication {
public static void main(String[] args) {
SpringApplication.run(DateEditorApplication.class, args);
}
/*@Bean
public DefaultBeanPostProcessor defaultBeanPostProcessor() { //此方法用于启用DefaultBeanPostProcessor,另一种方法是在DefaultBeanPostProcessor上加@Component注解
return new DefaultBeanPostProcessor();
}*/
}