Skip to content

Instantly share code, notes, and snippets.

@kshep92
Last active November 12, 2018 17:51
Show Gist options
  • Save kshep92/f417fcdd9b329d78051a15d0a86eb24d to your computer and use it in GitHub Desktop.
Save kshep92/f417fcdd9b329d78051a15d0a86eb24d to your computer and use it in GitHub Desktop.
@ControllerAdvice
public class NotFoundHandler {
@Value("${spa.default-file}")
String defaultFile;
@ExceptionHandler(NoHandlerFoundException.class)
public ResponseEntity<String> renderDefaultPage() {
try {
File indexFile = ResourceUtils.getFile(defaultFile);
FileInputStream inputStream = new FileInputStream(indexFile);
String body = StreamUtils.copyToString(inputStream, Charset.defaultCharset());
return ResponseEntity.ok().contentType(MediaType.TEXT_HTML).body(body);
} catch (IOException e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("There was an error completing the action.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment