spring mvc 之可选路径参数

在spring mvc中,注解@PathVariable可以获得路径参数,但如果我想让路径参数可选呢?

    @GetMapping({"/get/{offset}/{count}","/get/{offset}","/get/{offset}","/get"})
    public void getGoods(@PathVariable(required = false) Integer offset,@PathVariable(required = false) Integer count){
        System.out.println("offset:"+offset+"
count:"+count+"
");
    }
  • 1
  • 2
  • 3
  • 4

此时在这个例子中,offset和count都是可选的了,但是count存在时offset必须存在。

原文地址:https://www.cnblogs.com/suizhikuo/p/13628189.html