作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下@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/
我是一名优秀的程序员,十分优秀!