API REST con Spring Boot | 6 | Creando la capa Controlador
New Section
In this section, the speaker discusses the final layer of their app, which involves controllers and methods corresponding to services.
Creating Controllers and Methods
- The creation of a new class called "Persona" within the controls package is initiated.
- Declaration of the class as a Rest Controller using @RestController annotation and enabling access from different origins with @CrossOrigin annotation.
- Utilization of @RequestMapping to declare the URI for accessing resources, such as "/api/v1/personas".
New Section
This segment delves into injecting dependencies and commencing method implementation within the controller.
Dependency Injection and Method Implementation
- Declaring PersonServices within PersonaController's constructor for dependency injection.
- Initiating method creation starting with "getAll" corresponding to the service's "findAll" method.
New Section
Here, details about handling responses in JSON format are discussed within methods.
Handling Responses in Methods
- Implementing response handling using ResponseEntity to provide response status and data in JSON format.
- Defining HTTP status codes (e.g., OK, NOT FOUND) based on success or failure scenarios within methods.
New Section
This part focuses on generating error messages in JSON format for error scenarios.
Generating Error Messages
- Crafting error messages in JSON format with specific attributes like "error" and custom error messages.
Coding Tutorial Overview
In this coding tutorial, the speaker explains the process of creating a method in a tech environment. The tutorial covers utilizing blocks, declaring request types, handling responses, and creating controllers for an application.
Creating a Method with Blocks
- One person referred to as "el tic" is utilized within a block to set the status as 'ok' and employ the OCDE method for services that receive input.
- The URI is defined using curly braces and variables like 'aid' from the entity are declared as path variables.
- Parameters such as 'aire' are designated as request bodies.
Handling Responses and Errors
- A method named 'the lights' is created to handle responses where instead of 'ok', a message indicating no content is used for successful deletions.
- The delete method for persons is invoked by sending the input 'aire'.
- Error messages are managed by specifying error types and utilizing path variables.