【问题标题】:org.springframework.web.bind.MissingServletRequestParameterException : Required Long parameter 'userId' is not present"org.springframework.web.bind.MissingServletRequestParameterException:必需的长参数'userId'不存在“
【发布时间】: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


【解决方案1】:

您期望 userId 作为服务器端的请求参数,但您在客户端 http post 请求中将 userId 作为请求正文发送,

改变客户端发送userId作为请求参数

 $http.post('/portal/settings/user/deletecustomeruser?userId='+userId)

或更改服务器端以在控制器中使用 userId 作为请求正文

myControllerMethod (@RequestBody String userId)

【讨论】:

  • @RequestBody 注解没有 value 属性
猜你喜欢
  • 2021-03-28
  • 2016-09-03
  • 2020-10-16
  • 2017-10-26
  • 1970-01-01
  • 2019-09-23
  • 2021-04-01
  • 2018-05-03
  • 1970-01-01
相关资源
最近更新 更多