This is the first time I develop a REST API with Spring Boot. I want to return a custom message when I have a Bad Request 400 error.
I have my controller:
@GetMapping("/DetailDossier/{id},{parameters}/")
public ResponseEntity<List<DetailDossierRspn>> DetailDossierQstn(
@PathVariable(value = "id") String[] id, @PathVariable(value = "parameters") String parameters,
throws ParseException {
List<DetailDossierRspn> rspn = new ArrayList<>();
WSDetailDossierService mainDAO = new WSDetailDossierService();
// If Sql result return -> List<DetailDossierRspn>
// If no sql result return null
rspn = mainDAO.initialiserDAO(identifiant, coetb, null);
if (rspn == null) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
} else {
return ResponseEntity.ok(rspn);
}
}
I want the easiest way to return a 400 error:
ResponseEntity<>(HttpStatus.BAD_REQUEST)
with a custom message.
I tried to create a @ControllerAdvice, but it didn't work because my my controller return a List<Object>
Is there a way to easily create a custom message?
You need to create a custom
@ControllerAdvice
:extending
ResponseEntityExceptionHandler
:Basically something like: