在默认状况下,不加任何有关接收Date类型数据的配置时,前端传递Date类型数据至后端接口,控制台出现如下异常:前端
Failed to convert from type [java.lang.String] to type [java.util.Date] for value
'2333333333'; nested exception is java.lang.IllegalArgumentException]]
复制代码
也就是说,在SpringBoot
中,必须添加某种配置才能让前端正确传递时间类型数据到后端。下面针对不一样的状况来介绍一下解决办法。java
@DateTimeFormat
第一种状况,仅须要对某个Bean类的Date类型字段进行转换,那么只须要在Bean类属性上增长@DateTimeFormat()
注解,括号内 pattern
为前端传递的日期格式。好比:web
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
Date createTime;
复制代码
特色--缺陷:spring
按照如上配置,只能处理形如:2018-11-2 2:22:2
这样固定格式的数据,没法处理时间戳和2018-11-2
格式的数据。若是想要处理2018-11-2
这样的时间,就必须把pattern
改为yyyy-MM-dd
。后端
能够对不一样的属性赋予不一样的pattern
,可是对每一个Date类型都要加上注解显得比较繁琐,也没法处理单个属性可能对应不一样格式的值的状况。mvc
更通用、有效的方式是定义一个时间转换类,并将其应用到全部的接口上。app
分为两个步骤:ide
import org.springframework.core.convert.converter.Converter;
import org.springframework.util.StringUtils;
import java.text.SimpleDateFormat;
import java.util.Date;
/** * 日期转换类 * 将标准日期、标准日期时间、时间戳转换成Date类型 */
public class DateConverter implements Converter<String, Date> {
private static final String dateFormat = "yyyy-MM-dd HH:mm:ss";
private static final String shortDateFormat = "yyyy-MM-dd";
private static final String timeStampFormat = "^\\d+$";
@Override
public Date convert(String value) {
if(StringUtils.isEmpty(value)) {
return null;
}
value = value.trim();
try {
if (value.contains("-")) {
SimpleDateFormat formatter;
if (value.contains(":")) {
formatter = new SimpleDateFormat(dateFormat);
} else {
formatter = new SimpleDateFormat(shortDateFormat);
}
return formatter.parse(value);
} else if (value.matches(timeStampFormat)) {
Long lDate = new Long(value);
return new Date(lDate);
}
} catch (Exception e) {
throw new RuntimeException(String.format("parser %s to Date fail", value));
}
throw new RuntimeException(String.format("parser %s to Date fail", value));
}
}
复制代码
介绍两种方式:使用@Component
+ @PostConstruct
或@ControllerAdvice
+ @InitBinder
spa
第一种方式:.net
@Component
+ @PostConstruct
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import javax.annotation.PostConstruct;
/** * @author zfh * @version 1.0 * @date 2018/12/30 10:16 */
@Component
public class WebConfigBeans {
@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 DateConverterConfig());
}
}
}
复制代码
第二种方式:
@ControllerAdvice
+ @InitBinder
有关这两个注解的含义请参考个人博客:Spring进阶之@ControllerAdvice与统一异常处理
import com.aegis.config.converter.DateConverter;
import com.aegis.model.bean.common.JsonResult;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
/** * @author zfh * @version 1.0 * @since 2019/1/4 15:23 */
@ControllerAdvice
public class ControllerHandler {
@InitBinder
public void initBinder(WebDataBinder binder) {
GenericConversionService genericConversionService = (GenericConversionService) binder.getConversionService();
if (genericConversionService != null) {
genericConversionService.addConverter(new DateConverter());
}
}
}
复制代码
OK,关于SpringBoot处理前端日期传值的问题的相关解决办法就介绍到这里了。