get 请求接收方式

1.请求路径为
http://localhost:9092/hello?myParam=3&twoParam=2

接收
@RestController
public class HelloController {
// @GetMapping(“hello”)
@RequestMapping(value=“hello”,method = RequestMethod.GET)
public String helloController(@RequestParam(name = “myParam”) String myParam,@RequestParam(name=“twoParam”) String twoParam){
System.out.println();
return myParam + twoParam;
}
}
2.请求路径为
http://localhost:9092/hello/myParam/twoParam
@RestController

public class HelloController {
// @GetMapping(“hello”)
@RequestMapping(value=“hello/{myParam}/{twoParam}”,method = RequestMethod.GET)
public String helloController(@PathVariable(name = “myParam”) String myParam,@PathVariable(name=“twoParam”) String twoParam){
System.out.println();
return myParam + twoParam;
}
}

简单看下@PathVariable 和 @RequestParam的源码

@PathVariable

get 请求接收方式
get 请求接收方式

  • Annotation which indicates that a method parameter should be bound to a URI template
  • variable. Supported for {@link RequestMapping} annotated handler methods.
  • 注解用来表明方法参数绑定URI模板的变量,支持RequestMapping处理方法
    属性name 和value 互为别名,都可以使用

required
get 请求接收方式

默认为true,代表参数不存在会把报404,可能不存在的就改为false

@RequestParam类似,就不写了