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

查看:32
本文介绍了将 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'

我该如何解决这个问题??

How can I resolve this ??

推荐答案

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

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天全站免登陆