Handling Exceptions, Intro to Spring Data JPA Flashcards
What annotation is used to create a centralized exception handler in Spring?
@ControllerAdvice is used to create a centralized exception handler in Spring.
How can you handle a specific exception within a controller in Spring Boot?
By using @ExceptionHandler within the controller for the specific exception type, you can handle exceptions at the controller level in Spring Boot.
What’s the purpose of @ExceptionHandler in Spring?
@ExceptionHandler is used to define methods that handle specific exceptions thrown by request handling methods in Spring controllers.
What is the benefit of using centralized exception handling in Spring applications?
Centralized exception handling helps in separating exception handling logic from regular controller logic, making the code more organized and maintainable.
How do you define a custom exception in Java for use in Spring applications?
You can create a custom exception by extending the RuntimeException class or its subclasses.
In Spring Boot, what’s the recommended way to return custom error responses when an exception occurs?
Using ResponseEntity along with appropriate HTTP status codes and custom error messages is a recommended way to return custom error responses in Spring Boot.
What is @ControllerAdvice used for in Spring?
@ControllerAdvice is an annotation used in Spring to define global elements to be used across multiple controllers. It primarily allows centralized handling of exceptions and applying global configurations to all or selected controllers.
What types of concerns can be addressed using @ControllerAdvice in Spring?
@ControllerAdvice can handle various concerns such as exception handling, model attributes shared across multiple controllers, and applying response modifications.
Can you specify the scope of controllers affected by @ControllerAdvice?
Yes, you can narrow down the scope by specifying the base packages or specific controllers using the basePackageClasses or basePackages attribute within @ControllerAdvice.
Apart from handling exceptions, what other functionalities can @ControllerAdvice provide?
It can assist in manipulating the model attributes before they are added to the model by using @ModelAttribute annotated methods within @ControllerAdvice.
How does @ControllerAdvice relate to @ExceptionHandler?
@ExceptionHandler is one of the functionalities provided by @ControllerAdvice. It’s used to define methods that handle specific exceptions thrown by request handling methods in Spring controllers.
What is Object-Relational Mapping (ORM)?
ORM is a programming technique that facilitates the conversion of data between incompatible type systems—object-oriented programming languages and relational databases—by mapping objects to database tables.
How does ORM work?
ORM frameworks provide a layer of abstraction between the application and the database, enabling developers to interact with databases using object-oriented principles, where classes represent tables and instances represent rows.
What are the primary advantages of using ORM?
ORM simplifies database interactions by allowing developers to use programming language constructs instead of writing complex SQL queries, promotes code reusability, and abstracts database-specific implementation details.
What are some popular ORM frameworks or libraries?
Examples of ORM frameworks include Hibernate (for Java), Entity Framework (for .NET), Django ORM (for Python), Sequelize (for Node.js), and SQLAlchemy (for Python).