2

向服务器端发布调用时面临问题

异常堆栈跟踪:

"org.springframework.web.bind.MissingServletRequestParameterException: Required int parameter 'answerId' is not present\r\n\tat org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.raiseMissingParameterException(AnnotationMethodHandlerAdapter.java:773)\r\n\tat org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveRequestParam(HandlerMethodInvoker.java:509)

Javascript 调用controller.js

$scope.saveCorrectAnswer = function(answerId) {

        var answerIdVal = 0;
        answerIdVal = answerId;
        if(document.getElementById(answerId).className == 'ico-white-check') {
            $scope.answer.correct = 'Y';
        } else{
            $scope.answer.correct = 'N';
        }

        Answer.update({answerId: answerIdVal, correct: $scope.answer.correct}, function(response) {
            // On success go to Exchange
            //$route.reload();
        },

java中服务控制器中的映射:

@RequestMapping(method = RequestMethod.PUT, consumes = "application/json", produces = "application/json")
@ResponseBody
public void addCorrectAnswer(@RequestParam int answerId, @RequestParam String correct) {

    getAnswerDAC().addCorrectAnswer(answerId, correct);

}
4

1 回答 1

-1

@RequestParam 有一个required默认为 true 的属性。如果不需要 answerId,请按如下方式更改注解和参数类型...

@RequestMapping(method = RequestMethod.PUT, consumes = "application/json", produces = "application/json")
@ResponseBody
public void addCorrectAnswer(@RequestParam(required = false) Integer answerId, @RequestParam String correct) {
     getAnswerDAC().addCorrectAnswer(answerId, correct);
}

编辑:由于 answerId 在您的示例中是一个原始值,因此您还需要在注释中提供 defaultValue 。提供 defaultValue 隐式地将 required 设置为 false,所以我将把它排除在示例之外......

@RequestMapping(method = RequestMethod.PUT, consumes = "application/json", produces = "application/json")
@ResponseBody
public void addCorrectAnswer(@RequestParam(defaultValue = 0) int answerId, @RequestParam String correct) {
     getAnswerDAC().addCorrectAnswer(answerId, correct);
}

希望这可以帮助

于 2013-01-08T18:39:37.647 回答