浅析 SpringMVC 的@PathVariable 和 @RequestParam 和 @RequestBody注解

http://blog.csdn.net/chuck_kui/article/details/55506723

spring MVC中,@PathVariable 和 @RequestParam 两者的作用都是将request里的参数的值绑定到contorl里的方法参数里的,区别在于,URL写法不同。

使用@RequestParam时,URL是这样的:http://host:port/path?参数名=参数值

使用@PathVariable时,URL是这样的:http://host:port/path/参数值

@RequestBody的作用其实是将json格式的数据转为java对象,需要注意@RequestBody不能作用于get请求

前台
var data = {
          "data": {
                "name": methodName,
                "params": params
            }
      };
  $.ajax({
           url: basePath + '/open/interface/invoke',
           type: "POST",
           data: JSON.stringify(data),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (res) {
                                  
             }
          });

 

后台:
@PostMapping("/call")
 @ResponseBody
public void call(@RequestBody User user) {
       
 }

我们前台的两个json数据就会自动匹配到User这个对象中的属性中了,当然属性名称要一样

  

 

原文地址:https://www.cnblogs.com/gaomanito/p/8465721.html