spring
Spring Framework Exceptions
Exception types thrown by the Spring Framework and Spring Boot. Covers IoC container failures, web MVC errors, data access exceptions, security violations, and transaction errors.
20 codes
references docs.spring.io/spring-framework/docs/current/javadoc-api/docs.spring.io/spring-boot/docs/current/reference/htmlsingle/
· All codes 20 codes
- AccessDeniedException AccessDeniedException Thrown by Spring Security when an authenticated principal attempts an operation they are not authorised to perform. By default produces an HTTP 403 Forbidden response. Configure a custom AccessDeniedHandler to return a tailored error response.
- AuthenticationException AuthenticationException Base exception for Spring Security authentication failures. Subclasses include BadCredentialsException, DisabledException, LockedException, and AccountExpiredException. By default produces an HTTP 401 Unauthorized response handled by AuthenticationEntryPoint.
- BadCredentialsException BadCredentialsException Thrown by Spring Security's AuthenticationManager when the supplied credentials (e.g. username/password) do not match any account. Subclass of AuthenticationException; handled by AuthenticationFailureHandler, which by default redirects to the login page with an error parameter.
- BeanCreationException BeanCreationException Thrown when a bean cannot be created by the Spring IoC container. Usually wraps a more specific cause such as a missing dependency, constructor failure, or post-processor error. The message typically names the offending bean and configuration location.
- BeanCurrentlyInCreationException BeanCurrentlyInCreationException Thrown when a circular dependency is detected between beans during context startup. With constructor injection Spring cannot break the cycle automatically; switching one dependency to setter or field injection, or annotating it with @Lazy, resolves it.
- BeanDefinitionStoreException BeanDefinitionStoreException Thrown when the application context cannot read or parse bean definitions from a configuration file, annotation, or component scan. Often caused by malformed XML, an invalid @Configuration class, or a missing classpath resource referenced in @ImportResource.
- BeanInstantiationException BeanInstantiationException Thrown when Spring cannot instantiate a bean class, typically because the class is abstract, has no suitable constructor, or its constructor threw an exception during instantiation.
- DataIntegrityViolationException DataIntegrityViolationException Spring Data abstraction over database integrity constraint violations (unique constraints, foreign keys, not-null, check constraints). Wraps the driver-specific exception. Commonly triggered by a duplicate insert or a missing required foreign key reference.
- EmptyResultDataAccessException EmptyResultDataAccessException Thrown by JdbcTemplate and Spring Data repositories when a query expected to return a single result returns no rows. Prefer Optional return types or findById() over getById()/getOne() to handle the absent-result case without an exception.
- HttpClientErrorException HttpClientErrorException Thrown by RestTemplate when the remote server returns a 4xx HTTP response. Subclasses exist for common codes (e.g. HttpClientErrorException.NotFound for 404). Call getStatusCode() and getResponseBodyAsString() on the exception for details.
- HttpMessageNotReadableException HttpMessageNotReadableException Thrown when Spring MVC cannot deserialize the HTTP request body, typically because the JSON or XML is malformed or a required field has the wrong type. Returns 400 Bad Request by default. Check the request body format and the matching DTO class.
- HttpServerErrorException HttpServerErrorException Thrown by RestTemplate when the remote server returns a 5xx HTTP response. Analogous to HttpClientErrorException but indicates a server-side fault. Call getStatusCode() and getResponseBodyAsString() to inspect the failure.
- LazyInitializationException LazyInitializationException Thrown by Hibernate (via Spring Data JPA) when a lazily-loaded association is accessed outside an active Hibernate Session. Fix by annotating the calling method with @Transactional, using JOIN FETCH in the query, switching to eager loading, or using a DTO projection.
- MethodArgumentNotValidException MethodArgumentNotValidException Thrown by Spring MVC when an @RequestBody or @ModelAttribute argument annotated with @Valid or @Validated fails Bean Validation. The BindingResult inside the exception lists all field and global constraint errors. Results in 400 Bad Request by default.
- MissingServletRequestParameterException MissingServletRequestParameterException Thrown when a required @RequestParam is absent from the HTTP request. Results in a 400 Bad Request response by default. Make the parameter optional with required = false or supply a defaultValue to avoid this exception.
- NoSuchBeanDefinitionException NoSuchBeanDefinitionException Thrown when the application context cannot find a bean of the requested type or name. Common when a @Component/@Service/@Repository class is missing a stereotype annotation, is not in a scanned package, or when @Autowired targets an unregistered type.
- NoUniqueBeanDefinitionException NoUniqueBeanDefinitionException Thrown when more than one bean matches an @Autowired injection point and no single candidate is marked @Primary. Resolve by adding @Qualifier to name the intended bean, or annotate one implementation with @Primary.
- OptimisticLockingFailureException OptimisticLockingFailureException Thrown when a JPA @Version field detects a concurrent modification — another transaction updated the entity between the current transaction's read and write. Retry the operation, merge the conflicting changes, or inform the user of the conflict.
- TransactionSystemException TransactionSystemException Thrown when a transaction commit or rollback fails at the transaction manager level, after the application code has already completed. Often caused by a lost database connection during commit or a resource manager failure.
- UnsatisfiedDependencyException UnsatisfiedDependencyException Thrown when a required dependency for a bean cannot be satisfied during autowiring. Typically wraps a NoSuchBeanDefinitionException or a type mismatch, and the message identifies the injection point (field, constructor parameter, or setter).