【问题标题】:Request mapping in Spring boot with url-encoded charactersSpring Boot 中使用 url 编码字符的请求映射
【发布时间】:2019-05-08 06:46:37
【问题描述】:

在我的 Spring Boot 应用程序中,我有一个与来自客户端的值连接的 URL,例如:

/api/foo/{client-defined-value}/bar/

而真正的 URL 可能是这样的:

/api/foo/OBCH.%20Z%C3%81STUPCI/bar/

(不是url编码的值是“OBCH.ZÁSTUPCI”)

在控制器中我定义了 GET 请求映射:

@GetMapping(value = "/foo/{value:[^\\/]+}/bar/")

但未找到映射:

找不到带有 URI 的 HTTP 请求的映射...

我做错了什么?

【问题讨论】:

    标签: spring-boot tomcat8


    【解决方案1】:

    您可以通过使用注解@Pathvariable("client-defined-value") 作为方法参数来访问URL 变量。

    像这样:

    @RequestMapping(value = "/api/foo/{client-defined-value}/bar/")
    public void foo(@PathVariable("client-defined-value") String value) {
        doSomething…
    }
    

    【讨论】:

    • 请阅读我的问题。 HTTP 请求映射到特定控制器方法存在问题,而不是您所理解的从 URL 读取值。
    • 将您的请求映射更改为 @RequestMapping(value = "/api/foo/{client-defined-value}/bar/") ?这没有帮助吗?
    猜你喜欢
    • 2019-11-13
    • 2019-11-25
    • 2021-07-01
    • 2016-06-27
    • 1970-01-01
    • 2019-05-12
    • 1970-01-01
    • 1970-01-01
    • 2019-02-03
    相关资源
    最近更新 更多