Kum*_*dra 6 javascript java post spring spring-mvc
我在发布对Spring控制器的调用时遇到异常
例外情况是:
org.springframework.web.bind.MissingServletRequestParameterException : Required Long parameter 'userId' is not present"
Run Code Online (Sandbox Code Playgroud)
我的Javascript文件是:
$scope.removeUser = function(user){
var userId = JSON.stringify(user);
$http.post('/portal/settings/user/deletecustomeruser', userId).success(function(data){
$scope.response = data;
})
}
}
Run Code Online (Sandbox Code Playgroud)
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 );
Run Code Online (Sandbox Code Playgroud)
当我放(required = false),然后userId价值变成了null.JavaScript控制器显然"userId"是以JSON格式发送的,但是从控制器端出现了一些问题.
我在stackoverflow中检查了几乎所有问题,但给定的解决方案没有帮助.
您希望userId作为服务器端的请求参数,但是您在客户端http post请求中将userId作为请求主体发送,
更改客户端以将userId作为请求参数发送
$http.post('/portal/settings/user/deletecustomeruser?userId='+userId)
Run Code Online (Sandbox Code Playgroud)
或者更改服务器端以将userId用作控制器中的请求主体
myControllerMethod (@RequestBody String userId)
Run Code Online (Sandbox Code Playgroud)