尽管没有丢失值,但在Spring Boot上发出请求并获得缺少的参数异常。解决数独的项目
请求网址为:http://localhost:8080/solveSudoku/getCellAnswer/0/0
@GetMapping(value = "/getCellAnswer/{row}/{column}")
public Integer getCellAnswer(@RequestParam Integer row, @RequestParam Integer column) {
return service.solveCell(row, column);
}
下面是错误消息:
{
“时间戳记”:1567388255973,
“状态”:400,
“错误”:“错误请求”,
“例外”:“ org.springframework.web.bind.MissingServletRequestParameterException”,
“ message”:“不存在必需的整数参数'row'”,
“路径”:“ / solveSudoku / getCellAnswer / 0/0”
}
答案 0 :(得分:1)
您当前为此使用了错误的注释。使用@RequestParam
的请求将如下所示,只需使用@GetMapping(value = "/getCellAnswer")
:
http://localhost:8080/solveSudoku/getCellAnswer?row=0&column=0
可能您正在尝试使用@PathVariable
,并使用{row}
和{column}
定义模板。为此,您可以像下面那样更改代码:
@GetMapping(value = "/getCellAnswer/{row}/{column}")
public Integer getCellAnswer(@PathVariable Integer row, @PathVariable Integer column) {
return service.solveCell(row, column);
}
答案 1 :(得分:0)
使用 @PathVariable ,您的网址将类似于: http://localhost:8080/solveSudoku/getCellAnswerWithPath/1/2
您的代码就像:
@GetMapping(value = "/getCellAnswerWithPath/{row}/{col}")
public ResponseEntity<Integer> getCellAnswerWithPath(@PathVariable int row, @PathVariable int col) {
return new ResponseEntity<>(service.solveCell(row,col), HttpStatus.OK);
}
在这里,您必须添加带有正确的 HttpStatus 代码
的 ResponseEntity使用 @RequestParam ,您的网址将类似于: http://localhost:8080/solveSudoku/getCellAnswer?row=1&col=2
您的代码就像:
@GetMapping(value = "/getCellAnswer")
public ResponseEntity<Integer> getCellAnswer(@RequestParam(value="row") int row, @RequestParam(value="col") int col) {
return new ResponseEntity<>(service.solveCell(row,col), HttpStatus.OK);
在这里,您必须添加带有正确的 HttpStatus 代码
的 ResponseEntity这里是进口:
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
答案 2 :(得分:0)
您需要使用 PathVariable 并指定 PathVariable 名称
@GetMapping(value = "/getCellAnswer/{row}/{column}")
public Integer getCellAnswer(@PathVariable (name="row") Integer row,
@PathVariable (name="column") Integer column) {
return service.solveCell(row, column);
}