首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >学习Spring的@RequestBody和RequestParam

学习Spring的@RequestBody和RequestParam
EN

Stack Overflow用户
提问于 2010-07-26 17:32:00
回答 2查看 43.3K关注 0票数 16

我正在编辑一个使用Spring的web项目,我需要添加Spring的一些注释。我要添加的两个是@RequestBody@RequestParam。我已经四处打听了一下,发现了,但我仍然不完全理解如何使用这些注释。有人能举个例子吗?

EN

回答 2

Stack Overflow用户

发布于 2010-07-26 17:43:58

控制器示例:

代码语言:javascript
运行
复制
@Controller
class FooController {
    @RequestMapping("...")
    void bar(@RequestBody String body, @RequestParam("baz") baz) {
        //method body
    }
}

@RequestBody变量体将包含HTTP请求的主体

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

票数 18
EN

Stack Overflow用户

发布于 2015-10-06 22:01:23

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

例如,Spring RequestParam的角请求如下所示:

代码语言:javascript
运行
复制
$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注释参数链接到HTTP主体。参数值使用HttpMessageConverters转换为声明的方法参数类型。此注释指示应该将方法参数绑定到web请求的主体。

例如,Spring RequestBody的角请求如下所示:

代码语言:javascript
运行
复制
$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) {...

希望这能有所帮助。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3337350

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档