springboot的controller接口接收date类型数据为NULL时的处理

遇到的问题一

Controller接口中接收一个pojo对象,对象中有两个时间属性“开始时间”和“结束时间”为date类型,当前台js传过来的数据类型为string时,Controller无法接收,报如下错误

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Thu Apr 18 15:58:03 CST 2019
There was an unexpected error (type=Bad Request, status=400).
Validation failed for object='busiDemo'. Error count: 2

解决办法

于是在js中将时间字符串转换为data类型解决

var beginTime = $("#beginTime").val();
beginTime = checkValue(beginTime);
if(beginTime){
    beginTime = new Date(beginTime);
}else{
    beginTime = null;
}
       
var endTime = $("#endTime").val();
endTime = checkValue(endTime);
if(endTime){
    endTime = new Date(endTime);
}else{
    endTime = null;
}

遇到问题二

随之又发现当beginTime和endTime为null时,Controller也无法接收而报错

解决办法

查询后得知在Controller中加入如下方法可解决以上两个问题

    /**
     * 自定义方法绑定请求参数的Date类型
     *
     * @param request
     * @param binder
     * @throws Exception
     */
    @InitBinder
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        CustomDateEditor editor = new CustomDateEditor(df, true);//true表示允许为空,false反之
        binder.registerCustomEditor(Date.class, editor);
    }
--------------------- 
作者:mirfang 
来源:CSDN 
原文:https://blog.csdn.net/u012489412/article/details/83140906 
版权声明:本文为博主原创文章,转载请附上博文链接!

经测试,方法有效,问题得以解决。

由于以上方法不通用,需在每个Controller类中处理

通用解决方法

自定义方法绑定请求参数的Date类型

springboot:
只需在配置文件中加入以下配置

#自定义方法绑定请求参数的Date类型
spring.mvc.date-format=yyyy-MM-dd HH:mm:ss

springmvc:
实现converter接口

/**
 * 全局日期处理类
 * Convert<T,S>
 *         泛型T:代表客户端提交的参数 
 * String
 *         泛型S:通过convert转换的类型
 */
public class DateConvert implements Converter<String, Date> {

    @Override
    public Date convert(String stringDate) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return simpleDateFormat.parse(stringDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

}

配置springmvc.xml

<!-- 第三步:注册处理器映射器/处理器适配器 ,添加conversion-service属性-->
    <mvc:annotation-driven conversion-service="conversionService"/>
   
    <!-- 第二步: 创建convertion-Service ,并注入dateConvert-->
    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="converters">
            <set>
                <ref bean="dateConvert"/>
            </set>
        </property>
    </bean>
    <!-- 第一步:  创建自定义日期转换规则 -->   
    <bean id="dateConvert" class="zpark.convert.DateConvert"/>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值