Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java org.springframework.web.bind.MissingServletRequestParameterException_Java_Json_Spring_Spring Mvc_Angularjs - Fatal编程技术网

Java org.springframework.web.bind.MissingServletRequestParameterException

Java org.springframework.web.bind.MissingServletRequestParameterException,java,json,spring,spring-mvc,angularjs,Java,Json,Spring,Spring Mvc,Angularjs,在向服务器端发送呼叫时遇到问题 异常堆栈跟踪: "org.springframework.web.bind.MissingServletRequestParameterException: Required int parameter 'answerId' is not present\r\n\tat org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethod

在向服务器端发送呼叫时遇到问题

异常堆栈跟踪:

"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);

}

@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);
}

希望这有帮助

@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);
}

希望这对您有所帮助

在AJAX调用之前,answerIdVal的值是多少?您是否可以使用客户端调试器(如firebug)来查看该值是否已提交?另一种调试方法是通过删除
@RequestParam
来更改方法签名,并获取
HttpServletRequest
,查看参数是否存在于
请求中。getParameterMap()
。在AJAX调用之前,answerIdVal的值是多少?您是否可以使用客户端调试器(如firebug)来查看该值是否已提交?另一种调试方法是通过删除
@RequestParam
来更改方法签名,并获取
HttpServletRequest
,查看该参数是否存在于
request.getParameterMap()
中。