0

I'm getting this error when I try to create an instance of an Object and store it into a database:

org.springframework.web.bind.MissingServletRequestParameterException

The code for the method:

@PostMapping(path="accounts/add", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
public
@ResponseBody
String addAccount(@RequestParam String accountNumber, @RequestParam String accountName ) {
    Account account = new Account();
    account.setAccountName(accountName);
    account.setAccountNumber(accountNumber);
    accountRepository.save(account);
    return "Saved";
}

When I use PostMan and try to enter the JSON in, it gives me this message. What am I doing wrong?

Ryan Gross
  • 31
  • 4
  • The exception indicates that you're not providing the parameter `accountNumber`, `accountName`, or both. Doesn't the exception specify which one is missing? – Jeremy Mar 05 '17 at 06:23
  • It specifies that I am missing the accountNumber, which is boggling to me when I specify it in the JSON. – Ryan Gross Mar 05 '17 at 06:24
  • So, it works, it puts the data into the database. But, it puts all of it into one column, which is not what I want. Why does it do that, and what can I do to fix it? I actually took care of it on my own! Thanks for clarifying a few things! – Ryan Gross Mar 05 '17 at 06:32

1 Answers1

0

Since your POSTing (or PUTing?) JSON as the content body, you cannot use @RequestParam to deconstruct it. That is used for query or form parameters. You need to specify a single method parameter with @RequestBody, which is a class that looks something like the following.

public class Account {
    public String accountNumber;
    public String accountName;

    // setters and getters removed for brevity
}

See this answer for more info: @RequestBody and @ResponseBody annotations in Spring

Community
  • 1
  • 1
Jeremy
  • 22,188
  • 4
  • 68
  • 81