@RequestParam 和@RequestBody的使用方式

来源:互联网 发布:顾家北和慎小嶷 知乎 编辑:程序博客网 时间:2024/06/05 18:33

@RequestParam annotated parameters get linked to specific Servlet request parameters. Parameter values are converted to the declared method argument type. This annotation indicates that a method parameter should be bound to a web request parameter.

For example Angular request for Spring RequestParam(s) would look like that:

$http.post('http://localhost:7777/scan/l/register', {params: {"username": $scope.username, "password": $scope.password, "auth": true}}).                    success(function (data, status, headers, config) {                        ...                    })@RequestMapping(method = RequestMethod.POST, produces = "application/json", value = "/register")public Map<String, String> register(Model uiModel,                                    @RequestParam String username, @RequestParam String password, boolean auth,                                    HttpServletRequest httpServletRequest) {...

@RequestBody annotated parameters get linked to the HTTP request body. Parameter values are converted to the declared method argument type using HttpMessageConverters. This annotation indicates a method parameter should be bound to the body of the web request.

For example Angular request for Spring RequestBody would look like that:

$scope.user = {            username: "foo",            auth: true,            password: "bar"        };    $http.post('http://localhost:7777/scan/l/register', $scope.user).                        success(function (data, status, headers, config) {                            ...                        })@RequestMapping(method = RequestMethod.POST, produces = "application/json", value = "/register")public Map<String, String> register(Model uiModel,                                    @RequestBody User user,                                    HttpServletRequest httpServletRequest) {... 

Hope this helps.

0 0
原创粉丝点击