user13566276
user13566276

Reputation:

Configure spring boot to redirect certain errors on certain urls

So I have an airline app where it requires the user to enter search details first before going to the search results page.

/search -> /searchresult

How can I configure /searchresult to automatically redirect to /search upon error 500 on /searchresult?

I'm guessing spring security but I'm not entirely sure on how to get it done

Any help is appreciated!

Upvotes: 0

Views: 120

Answers (2)

user2349108
user2349108

Reputation: 33

You can create below class, and specify the types of exceptions you want to handle. The exceptions you have mapped will be handled by this class:

@ControllerAdvice
public class AppExceptions extends ResponseEntityExceptionHandler {

    @ExceptionHandler({ Exception.class })
    public ResponseEntity<Object> handleInternal(RuntimeException ex, WebRequest request) {
        logger.error("500 Status Code", ex);
        AppResponse response = new AppResponse(new Date(), "InternalError");

        return handleExceptionInternal(
                ex, response, new HttpHeaders(), HttpStatus.NOT_FOUND, request);
    }
}

Upvotes: 0

Shivendra Tiwari
Shivendra Tiwari

Reputation: 229

Catch the exception and return accordingly

ModelAndView modelAndView = null;
try {
} catch(Exception e) {
  modelAndView  = new ModelAndView("search");
}
return modelAndView;

Upvotes: 1

Related Questions