Java 学习 Spring 的 @RequestBody 和 @RequestParam

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3337350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 22:24:12  来源:igfitidea点击:

learning Spring's @RequestBody and @RequestParam

javaspringspring-mvc

提问by Josh

I'm editing a web project that uses Spring and I need to adding some of Spring's annotations. Two of the ones I'm adding are @RequestBodyand @RequestParam. I've been poking around a little and found this, but I still don't completely understand how to use these annotations. Could anyone provide an example?

我正在编辑一个使用 Spring 的 web 项目,我需要添加一些 Spring 的注释。我要添加的两个是@RequestBody@RequestParam。我一直在四处摸索,发现了this,但我仍然不完全了解如何使用这些注释。谁能提供一个例子?

回答by Arjan

Controller example:

控制器示例:

@Controller
class FooController {
    @RequestMapping("...")
    void bar(@RequestBody String body, @RequestParam("baz") baz) {
        //method body
    }
}

@RequestBody:variable body will contain the body of the HTTP request

@RequestBody变量 body 将包含 HTTP 请求的正文

@RequestParam:variable baz will hold the value of request parameter baz

@RequestParam变量 baz 将保存请求参数 baz 的值

回答by Patrik Bego

@RequestParamannotated 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.

@RequestParam注释参数链接到特定的 Servlet 请求参数。参数值被转换为声明的方法参数类型。该注解表示方法参数应该绑定到 Web 请求参数。

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

例如,Spring RequestParam(s) 的 Angular 请求将如下所示:

$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) {...

@RequestBodyannotated 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.

@RequestBody注释参数链接到 HTTP 请求正文。使用 HttpMessageConverters 将参数值转换为声明的方法参数类型。这个注解表明一个方法参数应该绑定到 Web 请求的主体。

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

例如,Spring RequestBody 的 Angular 请求看起来像这样:

$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.

希望这可以帮助。