I'm trying to handle missing json data in a POST request. My controller class
@Controller
@RequestMapping("/testMetrics")
public class TestMetricsEndPoint extends StatusEndpointHandler implements RestEndPoint<TestMetrics,String> {
@Autowired
private ObjectMapper mapper;
@Autowired
private TestMetricsService testMetricsService;
@Override
public Status get(String id) {
// TODO Auto-generated method stub
return null;
}
@Override
@RequestMapping(method = RequestMethod.POST,consumes = "application/json", produces = "application/json")
public @ResponseBody Status create(@RequestBody TestMetrics core, BindingResult bindingResult) {
try {
if(bindingResult.hasErrors()){
throw new InvalidRequestException("Add failed, Please try again ", bindingResult);
}
if((core.getGroupName()==""||core.getGroupName()==null)&&(core.getTestName()==null||core.getTestName()=="")){
throw new MissingParametersException(HttpStatus.BAD_REQUEST.value(),"Please provide all necessary parameters");
}
TestMetrics dataObject = testMetricsService.create(core);
return response(HttpStatus.CREATED.value(),dataObject);
}catch (MissingParametersException e) {
return response(HttpStatus.BAD_REQUEST.value(),e.getLocalizedMessage());
}
}
Extended class:
public class StatusEndpointHandler {
public Status response(Integer statusCode,Object data){
Status status = new Status();
status.setData(data);
status.setStatus(statusCode);
return status;
}
}
Implemented interface:
public interface RestEndPoint<T extends SynRestBaseJSON, ID extends Serializable> {
Status get(ID id);
Status create(T entity, BindingResult bindingResult);}
Please look at the highlighted part So, when i tried to test the result through POSTMAN, i'm getting status as 200 OK. I have no idea hot to solve it. please help me with this situation. How to get the correct status code.?