How do you create custom exceptions in Spring Boot?
Build a custom exception with error codes in Spring Boot and return consistent ProblemDetail JSON errors, including validation failures and safe error settings.
2,882 posts about Java: questions with answers, tutorials and articles, newest first.
Build a custom exception with error codes in Spring Boot and return consistent ProblemDetail JSON errors, including validation failures and safe error settings.
@ExceptionHandler turns exceptions thrown in Spring controllers into controlled HTTP responses. See local and global handlers, ProblemDetail and resolution order.
Add 404.html, 5xx.html or error.html under the error folder and Spring Boot renders them automatically. Includes exception mapping and safe configuration.
Customise Bean Validation messages in Spring: inline attributes, messages.properties keys, localisation, custom constraints and JSON error responses.
Validate form objects with @Valid and BindingResult to redisplay forms with field errors, and use a controller advice to return clean 400 responses from REST endpoints.
Add custom validation to Spring MVC with annotations, a Spring Validator and @InitBinder, and report errors for forms and REST APIs.
@Validated adds validation groups and class-level method validation to Spring. See service examples, groups for create and update, and how it differs from @Valid.
Use Jakarta Bean Validation annotations with @Valid and @Validated, then map failures to a clean 400 response with one exception handler.
@ModelAttribute binds form and query parameters to an object and adds it to the model. See examples, validation, and how it differs from @RequestBody.
Use @PathVariable to bind {placeholders} in a mapping to controller parameters. Covers regex constraints, optional variables, query parameters and UriTemplate.
Set custom response headers in Spring with ResponseEntity or HttpServletResponse, and apply headers globally with filters, Spring Security and CORS.
ResponseEntity lets a Spring MVC controller control the HTTP status, headers and body of a response. This guide shows the builders, caching headers and error handling.
Return Java objects as JSON in Spring MVC with @RestController, ResponseEntity and Jackson, and learn how to customise field names, formats and status codes.
Spring MVC picks JSON, XML or a custom format from the Accept header. See produces and consumes, the format parameter, custom converters and 406 handling.
The produces attribute selects a Spring MVC handler by the Accept header and sets the response type. See examples, class-level overrides and 406 causes.
Use consumes and produces on @RequestMapping and its shortcuts to control request and response media types, with content negotiation and common errors.
@RequestMapping routes requests by path, HTTP method, query parameters, headers and content types at once, and all given conditions must match.
Read request headers in Spring MVC with @RequestHeader. Covers optional headers, defaults, reading all headers with HttpHeaders, content negotiation and testing.
@RequestHeader binds HTTP request headers to Spring handler parameters with conversion, defaults and required checks. See examples and security tips.
Use @PatchMapping with JSON Merge Patch and Jackson to update only sent fields, clear fields with null, validate and avoid mass assignment.