I am trying to send bad request from spring boot rest for validation error. Below is my method
public ResponseEntity<?> getMyData(@Valid @ModelAttribute InputParms ipParms,BindingResult bindingResult ) {
MyObject obj=new MyObject ();
if(bindingResult.hasErrors()) {
return ResponseEntity.status(400).body(obj);
}
//my other code
}
On client side I have something like below. But it is throwing exception and code is not even reaching if block.
ResponseEntity<MyObject > resultResp=restTemplate.postForEntity(url,inputParam, MyObject .class);
if(resultResp.getStatusCode().equals(HttpStatus.OK)) {
//my success code
}else {
//my bad response code
}
In the same code if i send status 200, it is working. What is I am doing wrong here.