“HttpMessageNotReadableException Required request body is missing” when trying to make a post request

后端 未结 2 1682
心在旅途
心在旅途 2021-01-14 05:10

I have a controller class with a few methods one of which is a method that is supposed to take POST requests and create a new Account with the JSON from the body of that POS

相关标签:
2条回答
  • 2021-01-14 05:24

    So I changed @Requestbodys to a single @RequestBody like andreim said, it fixed my bad request error but for some reason the account username would always be null when the post request went through.

    I messed with a few thing, eventually leading me to swap the order of the Account constructor parameters from

    Account(String username, String password)
    

    to

    Account(String password, String username)
    

    This worked for some reason and let me make my post request with the proper values. Latter out of curiosity I decided to swap the parameters back to Account(String username, String password) and the post request still works as intended.

    I have no idea what I did to get rid of the null problem but it seams to have worked.

    0 讨论(0)
  • 2021-01-14 05:35

    You cannot use multiple @RequestBody. You need to wrap everything into a class that will be used to match your request body.

    The same is answered also here.

    There is also a JIRA issue for a feature request which was rejected.

    NOTE: if you want to write less, you can use @PostMapping instead of @RequestMapping(method = RequestMethod.POST).

    NOTE: @RequestParam and @PathVariable are used to extract data from URI, not from body.

    NOTE: the same is valid also for the equivalent [FromBody] attribute from ASP.NET WebAPI.

    Complete example:

    Bellow I created a working example similar to your case:

    Request DTO

    public class AccountCreateRequest {
    
        private String userName;
        private String password;
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    }
    

    Response DTO

    public class AccountCreateResponse {
    
        private String userName;
        private String password;
    
        public AccountCreateResponse() {
        }
    
        public AccountCreateResponse(String userName, String password) {
            this.userName = userName;
            this.password = password;
        }
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    }
    

    Controller

    @RestController
    @RequestMapping("/v1/account")
    public class AccountController {
    
        @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
        public @ResponseStatus(HttpStatus.CREATED) AccountCreateResponse add(@RequestBody() AccountCreateRequest account) {
            AccountCreateResponse response = new AccountCreateResponse(account.getUserName(), account.getPassword());
            return response;
        }
    }
    

    curl request

    curl -X POST --data '{"userName":"bepis", "password":"xyz"}' -H "Content-Type:application/json" http://localhost:8080/v1/account
    
    0 讨论(0)
提交回复
热议问题