【发布时间】:2020-08-05 04:07:59
【问题描述】:
我在 Spring Boot 1.5.9 中有以下@RestController 方法:
@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 中的斜线造成的。
如何在 Spring 中将 @PathVariable 用于我的 timeZoneId?
【问题讨论】:
-
你真的想把它放在路径中吗?在 REST 哲学中,路径变量应该代表资源。在这里,时区更适合作为查询参数(因为它只是一个格式化配置)。
-
为什么不使用 timeZoneId 作为请求参数?此问题的重复:stackoverflow.com/questions/2334787/…
-
它不一定是资源,例如:
/profile/{username}不是资源,您可以根据用户名检查主体。我大体上同意。到目前为止,我使用?timeZoneId,但这个问题应该被视为关于使用包含斜杠的路径变量的方式的一般问题。 -
回复您的评论,这可能会对您有所帮助:stackoverflow.com/questions/3235219/… 问题是,您不能在 url 中传递正斜杠和其他一些字符作为路径的一部分。
标签: java spring spring-boot spring-data path-variables