Lombok_Databases_REST API
Lombok_Databases_REST API
It achieves
this by using annotations to automatically generate commonly used code such as getters,
setters, constructors, equals, hashCode, toString methods, and more at compile time.
@Getter and @Setter annotations can be used on fields or classes to generate getter and
setter methods automatically.
@Data annotation generates all the boilerplate involved in a typical data class: getters for all
fields, setters for all non-final fields, toString, equals, and hashCode methods, and a constructor
for all final fields.
@Builder annotation provides a way to implement the builder pattern, which can be useful for
constructing complex objects.(Builder Class(.builder):- A static nested class that contains
methods for setting the fields of the outer class.)
The build method in the builder class is responsible for creating an instance of the outer class.
In case of Constructor You need to explicitly set default values within the constructor or initialize
fields at the point of declaration, but in case of Builder we need to give default values. So by
doing this we can create customized and complex objects (we need not to pass values for each
field the default value will be set builder method itself).
Without Lombok:
public User() {}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
'}';
}
With Lombok:
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private Long id;
private String name;
private String email;
}
You can check this boilerplate code in target folder after compilation
Validation in Spring Boot:- It ensures that the data coming into the application, typically via
user inputs in web forms, REST API calls, or other external sources, adheres to specific rules
and constraints before being processed.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
Example:- You can see @Age , @Email and @NotBlank are coming form validation and it will
check the incoming data is following the given criteria or not
import javax.validation.constraints.Email;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
● 1xx (Informational): The request was received, and the process is continuing.
● 2xx (Successful): The request was successfully received, understood, and accepted.
● 3xx (Redirection): Further action needs to be taken to complete the request.
● 4xx (Client Error): The request contains bad syntax or cannot be fulfilled.
● 5xx (Server Error): The server failed to fulfill a valid request.
1xx Informational
100 Continue: The server has received the request headers and the client should proceed to
send the request body.
101 Switching Protocols: The requester has asked the server to switch protocols, and the server
has agreed to do so.
2xx Successful
200 OK: The request was successful.
201 Created: The request was successful, and a new resource was created.
202 Accepted: The request has been accepted for processing, but the processing has not been
completed.
204 No Content: The server successfully processed the request, but there is no content to
return.
3xx Redirection
301 Moved Permanently: The resource requested has been permanently moved to a new URL.
302 Found: The resource requested is temporarily located at a different URL.
304 Not Modified: The resource has not been modified since the last request.
HTTP status codes are sent by the server in response to a client's request. The client typically
sends an HTTP request to the server, and the server processes this request and returns an
HTTP response, which includes a status code to indicate the outcome of the request. Here is a
breakdown of how this process works and who is responsible for sending these codes:
1. **Client:**
- The client is usually a web browser, a mobile app, or any other tool that sends HTTP
requests.
- It makes a request to the server for a specific resource or to perform an action (e.g.,
retrieving a webpage, submitting form data, etc.).
2. **Server:**
- The server receives the request from the client, processes it, and generates an appropriate
response.
- The server includes an HTTP status code in the response to inform the client about the
result of the request.
1. **Client Request:**
- The client sends an HTTP request to the server. This request includes a method (e.g., GET,
POST, PUT, DELETE), a URL, headers, and optionally a body (for methods like POST).
2. **Server Processing:**
- The server receives the request and processes it. This involves:
- Parsing the request.
- Authenticating and authorizing the client (if required).
- Performing the requested action (e.g., querying a database, processing form data,
retrieving a resource).
- Generating a response based on the outcome of the processing.
3. **Server Response:**
- The server sends an HTTP response back to the client. This response includes:
- A status line with the HTTP version and the status code.
- Headers providing additional information about the response.
- An optional body containing the response data (e.g., HTML, JSON, error message).