0

In my main class spring boot, I want to verify the information which are in the method parameters and do some logic to return JSON Object with attribute "code" which contains a value "0" But I'm getting the 400 bad request in postman.

enter image description here

enter image description here

deadshot
  • 7,656
  • 4
  • 13
  • 31
Sarah
  • 41
  • 6
  • 1
    Welcome to stackoverflow. Please read https://stackoverflow.com/help/how-to-ask on how to ask a good question. Hint a screenshot of code (not even included in the original question) isn't a good question. – M. Deinum Jul 09 '20 at 13:46
  • @Sara do you call this a question title "POST method and return JSON Object" – deadshot Jul 09 '20 at 13:53
  • this will help https://stackoverflow.com/a/33749674/9050514 – deadshot Jul 09 '20 at 13:57
  • I'm sorry I had no idea how to make a post in StackOverFlow Next time I will make sure to make my posts clear – Sarah Jul 09 '20 at 13:58
  • @Sara check the link i have posted it will solve your problem – deadshot Jul 09 '20 at 14:02

2 Answers2

1

You can not use @RequestBody twice at same request as there is only one. I guess you have an object called user with the fieldes as your json. You should change the method signiture to validateItems(@RequestBody User user) And your json to user:{... Fieldes and values}

Itay wazana
  • 150
  • 5
  • Is it necessery to do this logic in RestController because I did it into the main class ? – Sarah Jul 09 '20 at 14:21
  • It is necessary to put the logic at controller. I would suggest go through thi guide https://www.baeldung.com/rest-with-spring-series – Itay wazana Jul 09 '20 at 14:50
0

Create a Model class with parameters and pass the Model through the @RequestBody

Controller

@PostMapping(value = "/signin")
@ResponseBody
public String something(@RequestBody Model myModel) 
{
    return "";
}

Model

class Model {

  private String param1;
  private String param2;

  Model() {}

  Model(String param1, String param2) {
    this.param1 = param1;
    this.param2 = param2;
  }
}
Jagadesh
  • 1,923
  • 1
  • 12
  • 20