我有以下@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/