You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Scala使用cask框架时LocalDateTime参数报InputParser隐式值错误如何解决

问题原因

cask框架默认只提供了基础数据类型(String、Int、Boolean等)的表单参数解析器(InputParser/ParamReader隐式实例),java.time.LocalDateTime不属于默认支持的类型,因此编译器无法找到对应的隐式参数实例,导致编译报错。

解决方案

1. 自定义LocalDateTime隐式解析器

在你的MinimalApplication对象内添加如下代码,声明LocalDateTime的参数解析规则:

import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import cask.model.ParamReader

// 适配HTML datetime-local控件提交的ISO格式时间字符串
implicit val localDateTimeReader: ParamReader[LocalDateTime] = 
  ParamReader.StringReader.map(timeStr => LocalDateTime.parse(timeStr, DateTimeFormatter.ISO_LOCAL_DATE_TIME))

2. (可选)优化前端时间赋值格式

你当前前端直接调用LocalDateTime.now().toString赋值给datetime-local控件,默认输出会带秒甚至纳秒部分,部分浏览器对带秒的时间格式支持不好,建议修改前端赋值逻辑,只保留到分钟:

// 把原来的value := s"${LocalDateTime.now()}"替换为:
value := LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm"))

改完后重新编译即可解决报错,cask会自动将表单提交的时间字符串解析为LocalDateTime实例传入你的POST接口方法。

内容的提问来源于stack exchange,提问作者Frans Perkkola

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 02:45:00