gpt4 book ai didi

java - @MatrixVariable 与 @RequestParam 相比有哪些优势?

转载 作者:行者123 更新时间:2023-12-04 14:22:49 26 4
gpt4 key购买 nike

我正在阅读这篇文章 http://www.kscodes.com/spring-mvc/spring-mvc-matrix-variable-example/一个优点是您可以使用变量类型 Map对于矩阵变量,使用 @RequestParam 时不能使用此类型.但除此之外,我应该使用 @MatrixVariable 是否还有其他原因?而不是 @RequestParam ?

谢谢

最佳答案

不像 @RequestParam @MatrixVariable 由分号分隔 ;多个值用逗号分隔 , .阅读其文档:

Annotation which indicates that a method parameter should be bound to a name-value pair within a path segment. Supported for RequestMapping annotated handler methods.



有很多例子和用法的变化。下面是一些例子:
  • 网址:localhost:8080/person/Tom;age=25;height=175和 Controller :
    @GetMapping("/person/{name}")
    @ResponseBody
    public String person(
    @PathVariable("name") String name,
    @MatrixVariable("age") int age,
    @MatrixVariable("height") int height) {

    // ...
    }
  • 它甚至可以映射到 String[] .

    网址:localhost:8080/person/Tom;languages=JAVA,JAVASCRIPT,CPP,C和 Controller
    public String person(
    @PathVariable("name") String name,
    @MatrixVariable("languages") String[] languages) {

    // ...
    }
  • 关于java - @MatrixVariable 与 @RequestParam 相比有哪些优势?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52029191/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com