gpt4 book ai didi

java - org.springframework.web.bind.MissingServletRequestParameterException异常

转载 作者:行者123 更新时间:2023-11-29 09:30:44 25 4
gpt4 key购买 nike

向服务器端发送调用时遇到问题

异常堆栈跟踪:

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

controller.js 中的 Javascript 调用

$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服务 Controller 中的映射:

@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,默认为真。如果不需要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);
}

希望对你有帮助

关于java - org.springframework.web.bind.MissingServletRequestParameterException异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14218868/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com