在Spring Boot应用程序中,我在Controller
中有以下方法签名:
@PostMapping(value="/borrow")
public ResponseEntity<Void> postBorrowBook(@RequestBody String personId,
@RequestBody String bookId) {
LOG.info(RESTController.class.getName() + ".postBorrowBook() method called.");
...
return new ResponseEntity<Void>(HttpStatus.OK);
}
我想从RequestBody
获取两个参数的值。
如果我提出的请求是JSON,可以让我知道如何做到这一点:
{"personId":"207","bookId":"5"}
我目前正在接收:
{
"timestamp": "2018-06-17T20:59:37.330+0000",
"status": 400,
"error": "Bad Request",
"message": "Required request body is missing: public org.springframework.http.ResponseEntity<java.lang.Void> com.city2018.webapps.code.controller.RESTController.postBorrowBook(java.lang.String,java.lang.String)",
"path": "/rest/borrow/"
}
对于简单的非REST请求,我已经在类似的场景中进行了以下工作:
@RequestMapping(value="/borrow", method=RequestMethod.POST)
public String postBorrowBook(@RequestParam("personId") String personId,
@RequestParam("bookId") String bookId,
Model model) {
LOG.info(PersonController.class.getName() + ".postBorrowBook() method called.");
答案 0 :(得分:0)
您可以使用2个字段( personId 和 bookId )声明POJO并更改您的签名,如下所示:
echo $UID
1000
答案 1 :(得分:0)
首先,您应该将POJO类定义为:
public class BorrowBookEntity { public String personId; public String bookId; }
然后你依靠spring来获得值:
@PostMapping("/request")
public ResponseEntity<Void> postController(
@RequestBody BorrowBookEntity borrowBookEntity) {
...
答案 2 :(得分:0)
您也可以尝试另一个。 例如@RequestBodyParam
@RequestMapping(value = "/saveData", headers="Content-Type=application/json", method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<Boolean> saveData(@RequestBodyParam String source,@RequestBodyParam JsonDto json) throws MyException {
...
}