faserx
faserx

Reputation: 317

Spring MVC Validation error message

I have a problem with the validation in spring MVC. If I send an invalid request for the body, the application returns to the bad request but not my custom message.

The code is :

public @ResponseBody ResponseEntity<BaseModel> newTicket(@Valid @RequestBody Ticket ticket, Locale locale, BindingResult bindingResult ){
    if(bindingResult.hasErrors()) {
        logger.info("error");
        throw new BadRequestException("Bad Request");
    }

if I try to put the application in debug mode, the if is never executed.

Can someone help me?

Upvotes: 0

Views: 75

Answers (1)

NiVeR
NiVeR

Reputation: 9786

You can direct the bad request error in the following way:

@ExceptionHandler
@ResponseStatus(HttpStatus.BAD_REQUEST)
public void handle(HttpMessageNotReadableException e) {
    //do something here in case of bad request.
}

You can go on and customize according to your needs.

Upvotes: 1

Related Questions