Spring Boot Web app - Redirect controller
<p>I need to have a controller (or other component) that handles all 404 errors and smartly redirects to the correct page (this is based on a table src/ target). I have found a few questions about handling thrown exceptions FROM controllers so I have made the following:</p>

<pre><code>@ControllerAdvice
public class ServiceExceptionHandler extends ResponseEntityExceptionHandler {

    @ExceptionHandler(Throwable.class)
    @ResponseBody
    ResponseEntity&lt;String&gt; handleControllerException(HttpServletRequest req, Throwable ex) {

        String slug = req.getRequestURI(); // this is the URL that was not found
        URI location=null;
        try {
            // lookup based on slug ...
            location = new URI("http://cnn.com"); // let's say I want to redirect to cnn
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.setLocation(location);
        return new ResponseEntity&lt;String&gt;("Page has permanently moved", responseHeaders, HttpStatus.PERMANENT_REDIRECT);


    }
}
</code></pre>

<p>I have not made any other configuration changes</p>

<p>The two issues with this are:</p>

<ul>
<li>It only catches exceptions thrown by my other controllers and not 404 errors</li>
<li>It catches ALL types of exceptions and not just 404</li>
</ul>

<p>Any ideas on how to implement a kind of "catch-all"?</p>