springMVC报类型转换错误,常用解决方案就是将类型转换放到方法体内

DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String businessTime = dateformat.format(registerCheck.getApproveTime());
Date officeTime= subcompanyParamService.getAppointOfficeBusinessTime(businessTime,subCompanyID).getRes();
addPerson.setOfficeTime(officeTime);

//wx端调用H5端时,使用feign,其中传递了参数DATE,报错org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert val........Failed to convert from type [java.lang.String] to type [java.util.Date]

//两种方式可以解决
/**2种解决办法:

1. 参数改为String类型,在方法体内处理String到Date的转换,而不是交给SpringMVC来解析(即放在controller层的方法的参数列表中)。

2 写转换类实现org.springframework.core.convert.converter.Converter,并在SpringMVC中注册。

该Converter接口较为简单,代码略
**/

//此时只需要换类型即可,将DATA转为String 然后,String在转换为Date.

//feign端
@FeignClient(contextId = "subcompanyParamService", value = ServiceNameConstant.PLATFORM_SYSTEM)
    @GetMapping("/ParamValue/getAppointOfficeBusinessTime")
    ResponseNew<Date> getAppointOfficeBusinessTime(@RequestParam("dateString") String dateString, @RequestParam("subcompanyID") Integer subcompanyID);
}
//MVC端()
@ApiOperation("获取指定入职时间时间营业参数")
@GetMapping("/getAppointOfficeBusinessTime")
public ResponseNew<Date> getAppointOfficeBusinessTime(String dateString,Integer subcompanyID) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = new Date();
    try {
        date = sdf.parse(dateString);
    } catch (ParseException e) {
        // TODO: handle exception
        e.printStackTrace();
    }
    return success(resourceParamHandler.getAppointOfficeBusinessTime(date,subcompanyID));
}
//handler层
public Date getAppointOfficeBusinessTime(Date date, Integer subcompanyID){
    ...............
}

ps:后面两段代码主要针对根据目前时间获取对应营业日的方案

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值