将localDateTime字符串正确解析为spring boot @pathVariable [英] parse localDateTime string correctly into spring boot @pathVariable

查看:470
本文介绍了将localDateTime字符串正确解析为spring boot @pathVariable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取具有时间戳的用户的所有数据:

I'm trying to get all data of a user of a user with a timestamp:

@GetMapping("/datum/{userID}/{timeStamp}")
    List<Datum> getDataSingleUserTimeRange(@PathVariable Long userID, @PathVariable LocalDateTime timeStamp)
    {
          ....
    }

现在要测试这个Spring Boot rest api,在邮递员中,我调用了GET和url- http://localhost:8080/datum/2/2019-12-15T19:37:15.330995 .

Now to test this Spring Boot rest api, in postman, I made this call GET and url - http://localhost:8080/datum/2/2019-12-15T19:37:15.330995.

但是它给我错误提示:无法将类型'java.lang.String'的值转换为所需类型'java.time.LocalDateTime'

But it gives me error saying : Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDateTime'

我该如何解决??

推荐答案

我不知道这是否是最谦虚的方法,但这是我所做的:

I don't know if it is the most modest way to do this or not, but here is what I have done :

@GetMapping("/datum/{userID}/{timeStamp}")
    List<Datum> getDataSingleUserTimeRange(@PathVariable Long userID, @PathVariable String timeStamp)
    {
        DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
        LocalDateTime dateTime = LocalDateTime.parse(timeStamp, formatter);
        ...
        return datumRepository.findUsingTime(start,end);
    }

作为字符串传递并进行了解析.并且dateTime.truncatedTo(ChronoUnit.NECESARRY_UNIT);也可以使用.

Passed as string and parsed that. AnddateTime.truncatedTo(ChronoUnit.NECESARRY_UNIT); can be used as well.

这篇关于将localDateTime字符串正确解析为spring boot @pathVariable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆