我正在编辑一个使用Spring的web项目,我需要添加Spring的一些注释。我要添加的两个是@RequestBody和@RequestParam。我已经四处打听了一下,发现了这,但我仍然不完全理解如何使用这些注释。有人能举个例子吗?
发布于 2010-07-26 17:43:58
控制器示例:
@Controller
class FooController {
@RequestMapping("...")
void bar(@RequestBody String body, @RequestParam("baz") baz) {
//method body
}
}@RequestBody变量体将包含HTTP请求的主体
@RequestParam变量baz将保存请求参数baz的值。
发布于 2015-10-06 22:01:23
@RequestParam注释参数链接到特定的Servlet请求参数。参数值转换为声明的方法参数类型。此注释表示方法参数应绑定到web请求参数。
例如,Spring RequestParam的角请求如下所示:
$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的角请求如下所示:
$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) {...希望这能有所帮助。
https://stackoverflow.com/questions/3337350
复制相似问题