Starter for building web, including RESTful, applications using Spring MVC with embedded Tomcat server.
—
Built-in error handling with customizable error pages, API responses, and global exception handling.
Configure error page behavior and response details.
# Error page configuration
server.error.path=/error
server.error.include-exception=false
server.error.include-stacktrace=NEVER
# Options: NEVER, ALWAYS, ON_PARAM
server.error.include-message=NEVER
server.error.include-binding-errors=NEVER
server.error.include-path=ALWAYS
# Whitelabel error page
server.error.whitelabel.enabled=trueHandle exceptions globally across all controllers.
/**
* Global exception handler for all controllers
*/
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(ResourceNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public ResponseEntity<ErrorResponse> handleNotFound(ResourceNotFoundException ex) {
ErrorResponse error = new ErrorResponse("RESOURCE_NOT_FOUND", ex.getMessage());
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(error);
}
@ExceptionHandler(ValidationException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ResponseEntity<ErrorResponse> handleValidation(ValidationException ex) {
ErrorResponse error = new ErrorResponse("VALIDATION_ERROR", ex.getMessage());
return ResponseEntity.badRequest().body(error);
}
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ResponseEntity<ErrorResponse> handleGeneral(Exception ex) {
ErrorResponse error = new ErrorResponse("INTERNAL_ERROR", "An unexpected error occurred");
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error);
}
}
/**
* Exception handler for specific controller
*/
@RestController
public class UserController {
@ExceptionHandler(UserNotFoundException.class)
public ResponseEntity<String> handleUserNotFound(UserNotFoundException ex) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("User not found: " + ex.getMessage());
}
}Standard error response formats and attributes.
/**
* Default error attributes provided by Spring Boot
*/
public class DefaultErrorAttributes implements ErrorAttributes {
// Standard error attributes:
// - timestamp: Date/time of error
// - status: HTTP status code
// - error: HTTP status reason phrase
// - path: Request path
// - message: Exception message (configurable)
// - exception: Exception class name (configurable)
// - trace: Stack trace (configurable)
}
/**
* Custom error response class
*/
public class ErrorResponse {
private String code;
private String message;
private long timestamp;
private String path;
private Map<String, Object> details;
// Constructors, getters, setters
}RFC 9457 Problem Details for HTTP APIs.
/**
* Enable Problem Details support
*/
@Configuration
@EnableWebMvc
public class WebConfig {
@Bean
public ProblemDetailsExceptionHandler problemDetailsExceptionHandler() {
return new ProblemDetailsExceptionHandler();
}
}
/**
* Problem Details response structure
*/
public class ProblemDetail {
private URI type; // Problem type URI
private String title; // Short, human-readable summary
private int status; // HTTP status code
private String detail; // Human-readable explanation
private URI instance; // URI reference for this occurrence
private Map<String, Object> properties; // Additional properties
}Override default error handling behavior.
/**
* Custom error controller replacing default BasicErrorController
*/
@RestController
public class CustomErrorController implements ErrorController {
private final ErrorAttributes errorAttributes;
@RequestMapping("/error")
public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
Map<String, Object> body = getErrorAttributes(request);
HttpStatus status = getStatus(request);
return new ResponseEntity<>(body, status);
}
private Map<String, Object> getErrorAttributes(HttpServletRequest request) {
WebRequest webRequest = new ServletWebRequest(request);
return errorAttributes.getErrorAttributes(webRequest, ErrorAttributeOptions.defaults());
}
private HttpStatus getStatus(HttpServletRequest request) {
Integer statusCode = (Integer) request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
return statusCode != null ? HttpStatus.valueOf(statusCode) : HttpStatus.INTERNAL_SERVER_ERROR;
}
}// Error attribute options for controlling error response content
public class ErrorAttributeOptions {
public static ErrorAttributeOptions defaults();
public static ErrorAttributeOptions of(Include... includes);
public enum Include {
EXCEPTION, STACK_TRACE, MESSAGE, BINDING_ERRORS
}
}
// Exception handler annotation
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ExceptionHandler {
Class<? extends Throwable>[] value() default {};
}
// Controller advice annotation for global exception handling
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Controller
public @interface ControllerAdvice {
String[] value() default {};
String[] basePackages() default {};
Class<?>[] basePackageClasses() default {};
Class<?>[] assignableTypes() default {};
Class<? extends Annotation>[] annotations() default {};
}Install with Tessl CLI
npx tessl i tessl/maven-org-springframework-boot--spring-boot-starter-web