spring http请求的各种方式

1.RequestMapping包含get,post,put等请求,通过method = RequestMethod.POST指定请求方式,默认是get,@PathVariable代表从路径获取变量,变量是对象或者json使用@RequestBody @RequestMapping("/cluster/{clusterName}/flink/info") public ResultBean<?> queryClusterSqoopHost(@PathVariable String clusterName) {}

@RequestMapping(value = “/cluster/{clusterName}/config/{serviceName}/{hostName}”, method = RequestMethod.POST) public ResultBean<?> updateServiceHostConfig(@PathVariable String clusterName, @RequestBody JSONObject param) {}

2.get请求 @GetMapping("/find") public User findOne(@RequestParam(value = “name”, defaultValue = “yanjun”) String name) public User findOne(String name)

3.post请求 @PostMapping("/find") public User findOne(@RequestParam(value = “name”, defaultValue = “yanjun”) String name) public User findOne(String name)

4.put请求 @PutMapping("/find") public User findOne(@RequestParam(value = “name”, defaultValue = “yanjun”) String name) public User findOne(String name)

5.delete请求 @DeleteMapping("/remove") public User findOne(@RequestParam(value = “name”, defaultValue = “yanjun”) String name) public User findOne(String name)

6RequestParam指定参数是否是必传的,默认是false,参数为null时,给默认值。get,post,put等氢气与都可以使用 @RequestParam(value = “perfix”, required = false,defaultValue = “yanjun”) String prefix

7 @RequestBody,无论什么请求,参数如果是对象或者json,需要加上@RequestBody @PutMapping("/edit") public BaseResponseResult editSave( @RequestBody SysDictData sysDictData) {

8. @PathVariable 指定参数从请求路径获取 @GetMapping("/type") public ResponseResult getType(@PathVariable String dictType) {

经验分享 程序员 微信小程序 职场和发展