gpt4 book ai didi

java - 在 Spring Boot 中将日期作为请求参数处理出现错误

转载 作者:行者123 更新时间:2023-12-02 10:19:14 25 4
gpt4 key购买 nike

我正在处理一个要求,我将 keydate 作为 @RequestParam 传递,以获取之间的记录列表来自 MSSQL Server 数据库的 created_datemodified_date

here key is mandatory and date is optional.

我有一个GET请求,它将keydate作为@RequestParam发送到Spring Controller 。

@GetMapping(value = "/getValueFromGC", produces = { MediaType.APPLICATION_JSON_VALUE })
public List<GcAppConfig> getValueFromGC(@RequestParam String key, @RequestParam(required = false)@DateTimeFormat(pattern = "dd-MM-yyyy") Date date) {
//if date param is not null
if(date != null) {
return gcAppConfig.getValueFromGC(key,date);
}
//if date param is null fetching the list of all available records
else {
return gcAppConfig.getValueFromGC(key);
}

}

如果date参数为空, Controller 将获取记录。即

URL: localhost:8082/getValueFromGC?key=TOLERANCE&date=

同时以以下格式传递日期参数

Url:getValueFromGC?key=TOLERANCE&date=21-01-2019

这是我的服务gcAppConfig,它绕过date来获取created_datemodified_date之间的记录

Note:created_date and modified_date are in datetime2 format

@Query(" Select g from GcAppConfig g where g.isActive=1 and g.keys= :keys" + 
" and :date >=" +
" CAST(g.createdDate AS DATE) and " +
" :date <= " +
" CAST(g.modifiedDate AS DATE) ")
public List<GcAppConfig> getValueFromGC(@Param("keys")String keys, @Param("date")Date date);

Could not resolve requested type fo CAST: DATE

如有错误,还请在查询中指正。欢迎任何建议。提前致谢。

最佳答案

它与您的存储库无关。错误发生在Controller 级别。您真的必须使用 java.sql.Date 吗?如果您使用 java.util.Date ,它应该可以正常工作,特别是使用参数的 @DateTimeFormat("yyyy-MM-dd") 注释。

关于java - 在 Spring Boot 中将日期作为请求参数处理出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54455548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com