【问题标题】:How to use @Pathvariable and @RequestParam at the same request如何在同一请求中使用 @Pathvariable 和 @RequestParam
【发布时间】:2023-03-11 14:49:01
【问题描述】:

我正在尝试这段代码:

请求网址:GET:/produtName/v1/00000000123?model=1,2

@GetMapping(value="/{product}/{version}/{document}?model={model}) 
    public ResponseEntity<String> test(
            @PathVariable("product") String product,    
            @PathVariable("version") String version, 
            @PathVariable("document") String document,
            @RequestParam("model") List<String> model) {

但它不起作用。我做错了什么?

【问题讨论】:

    标签: java spring rest


    【解决方案1】:

    删除?model={model} 部分。它不是必需的。如果您将其留在路径中,您将无法调用此 API 方法。

    我也不确定您是否可以将@RequestParam 绑定到列表。您应该为此使用@MatrixVariable

    编辑:根据 SO 线程 Binding a list in @RequestParam 中的回答者,可以将 List@RequestParam 一起使用。看看这些答案也是值得的。

    【讨论】:

    • 谢谢! @莫里斯
    • @Aline 也请检查 this SO thread 关于在 requestparam 中绑定列表。根据回答者的说法,可以将列表绑定到 requestparam。
    • @Maurice,我认为如果您尝试使用 @MatrixVariable 它将不起作用,因为默认情况下 spring 禁用了分号分隔。你可能需要关注这篇文章baeldung.com/spring-mvc-matrix-variables#configuration
    • @stergipe 您也可以将@MatrixVariable 与逗号一起使用。
    猜你喜欢
    • 2014-08-16
    • 2015-08-28
    • 1970-01-01
    • 1970-01-01
    • 2012-11-22
    • 2015-08-18
    • 1970-01-01
    相关资源
    最近更新 更多