Skip to content

Instantly share code, notes, and snippets.

@itzg
Last active November 20, 2019 18:02
Show Gist options
  • Select an option

  • Save itzg/ae402158e9952be2d45f78a6196e8f7f to your computer and use it in GitHub Desktop.

Select an option

Save itzg/ae402158e9952be2d45f78a6196e8f7f to your computer and use it in GitHub Desktop.
Spring MVC controller advice for handling custom exceptions
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.ServletWebRequest;
@ControllerAdvice
@ResponseBody
public class RestExceptionHandler {
private final ErrorAttributes errorAttributes;
@Autowired
public RestExceptionHandler(ErrorAttributes errorAttributes) {
this.errorAttributes = errorAttributes;
}
@ExceptionHandler({NotFoundException.class})
public ResponseEntity<?> handleCustomExceptions(
HttpServletRequest request) {
return respondWith(request, HttpStatus.NOT_FOUND);
}
private ResponseEntity<?> respondWith(HttpServletRequest request,
HttpStatus status) {
Map<String, Object> body = getErrorAttributes(request);
body.put("status", status.value());
return new ResponseEntity<>(body, status);
}
private Map<String, Object> getErrorAttributes(HttpServletRequest request) {
final ServletWebRequest webRequest = new ServletWebRequest(request);
return errorAttributes.getErrorAttributes(webRequest, false);
}
}
@itzg
Copy link
Copy Markdown
Author

itzg commented Jun 5, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment