java spring mvc requestmapping动态url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27574537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 12:01:40  来源:igfitidea点击:

spring mvc requestmapping dynamic url

javaspringspring-mvccontroller

提问by user3795951

How do I change my request mapping for dynamic urls? URLs might look like this:

如何更改动态 url 的请求映射?URL 可能如下所示:

Here's the working controller syntax when the url is in this format:

这是当 url 为这种格式时的工作控制器语法:

http://zz.zz.zz.com:8080/webapp/test?Id=2&maxrows=5

http://zz.zz.zz.com:8080/webapp/test?Id=2&maxrows=5

@RequestMapping(value = "/test", method = RequestMethod.GET)
public @ResponseBody void test(
    @RequestParam(value = "Id", required = true) String Id,
    @RequestParam(value = "maxrows", required = true) int maxrows
) throws Exception {
        System.out.println("Id: " + Id + " maxrows: " + maxrows);
}

回答by Lukas Risko

Try this:

试试这个:

@RequestMapping(value = "/test/{param1}/{param2}/{param3}")
public @ResponseBody void test(
    @RequestParam(value = "Id", required = true) String Id,
    @RequestParam(value = "maxrows", required = true) int maxrows,
    @PathVariable(value = "param1") String param1,
    @PathVariable(value = "param2") String param2,
    @PathVariable(value = "param3") String param3) {
    ...
}

For more information look at Spring Reference Documentation

有关更多信息,请查看Spring 参考文档