简体   繁体   English

如何在Spring REST控制器方法中从RequestBody获取参数

[英]How to get parameters from RequestBody in Spring REST controller method

In a Spring Boot application I have the following method signature in a Controller : 在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);        
}      

I want to get the values of the two parameters from the RequestBody . 我想从RequestBody获取两个参数的RequestBody

Can anyone let me know how this be done if the request I am making is JSON as follows: 如果我发出的请求如下所示,有人可以让我知道如何完成此操作:

{"personId":"207","bookId":"5"}

I am currently receiving: 我目前收到:

{
"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/"
}

I already have the following working in a similar scenario for simple non-REST requests: 对于简单的非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.");  

You can declare a POJO with 2 fields ( personId and bookId ) and change your signature as follow: 您可以声明具有2个字段( personIdbookId )的POJO并按以下方式更改签名:

@PostMapping(value="/borrow")
public ResponseEntity<Void> postBorrowBook(@RequestBody RequestDTO requestBody) {
    requestBody.getPersonId();
    requestBody.getBookId();
    ...
}
  1. First you should define a POJO class as: 首先,您应该将POJO类定义为:

    public class BorrowBookEntity{ public String personId; 公共类BorrowBookEntity {public String personId; public String bookId; public String bookId; } }

  2. Then you rely on spring to get the value as: 然后,您依靠spring获得值为:

     @PostMapping("/request") public ResponseEntity<Void> postController( @RequestBody BorrowBookEntity borrowBookEntity) { ... 

You can also try another . 您也可以尝试其他。 eg @RequestBodyParam 例如@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 {
    ...
}

https://github.com/LambdaExpression/RequestBodyParam https://github.com/LambdaExpression/RequestBodyParam

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM