【发布时间】:2015-11-20 03:59:27
【问题描述】:
我在发布对 Spring 控制器的调用时遇到异常
例外是:
org.springframework.web.bind.MissingServletRequestParameterException : Required Long parameter 'userId' is not present"
我的 Javascript 文件是:
$scope.removeUser = function(user){
var userId = JSON.stringify(user);
$http.post('/portal/settings/user/deletecustomeruser', userId).success(function(data){
$scope.response = data;
})
}
}
Spring 控制器代码为:
@RequestMapping( value = "/user/deletecustomeruser", method = RequestMethod.POST , headers="Accept=application/json")
public @ResponseBody String deleteCustomerUser( @RequestParam (value = "userId") Long userId,
HttpServletRequest request )
throws Exception
{
String returnString = "";
User user = userService.getUserById( userId );
当我输入(required = false) 时,userId 的值变成了null。
JavaScript 控制器显然是以 JSON 格式发送 "userId",但从控制器端来看,存在一些问题。
我检查了 stackoverflow 中的几乎所有问题,但给出的解决方案没有帮助。
【问题讨论】:
-
你能显示正在发布的 JSON。我认为您发布的 JSON 格式不正确。您只发送价值。您需要以键/值对的形式发送它,例如
{"userId":123}
标签: javascript java spring spring-mvc post