gpt4 book ai didi

java - 如何在 Spring 传递 @PathVariable ("timeZoneId")?

转载 作者:行者123 更新时间:2023-12-02 08:41:33 25 4
gpt4 key购买 nike

我有以下@RestController Spring Boot 1.5.9 中的方法:

@GetMapping(value = "/today/{timeZoneId}", produces = MediaType.APPLICATION_JSON_VALUE)
public Instant getToday(@PathVariable("timeZoneId") String timeZoneId) {
return getNextPublishingDateTime(Instant.now(), timeZoneId);
}

当我 GET/today/Europe/Paris ,我有一个404错误。

我尝试GET/today/Europe%2FParis但还得到了404 .

这是由于 timeZoneId 中的斜线造成的.

如何使用@PathVariable对于我的timeZoneId在 Spring ?

最佳答案

一种可能的方式如下,

@GetMapping(value = "/today/{timeZoneIdPrefix}/{timeZoneIdSuffix}", produces = MediaType.APPLICATION_JSON_VALUE)
public Instant getToday(@PathVariable("timeZoneIdPrefix") String timeZoneIdPrefix,@PathVariable("timeZoneIdSuffix") String timeZoneIdSuffix) {
String timeZoneId = timeZoneIdPrefix +"/"+ timeZoneIdSuffix;
return getNextPublishingDateTime(Instant.now(), timeZoneId);
}

还有一种方法是,不要像 Europe/Paris 那样传递,而是像 Europe-Paris 那样传递,然后将 - 替换为 /

 return getNextPublishingDateTime(Instant.now(), timeZoneId.replace("-","/"));

关于java - 如何在 Spring 传递 @PathVariable ("timeZoneId")?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61360014/

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