0% found this document useful (0 votes)
19 views

Lombok_Databases_REST API

Lombok Database
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Lombok_Databases_REST API

Lombok Database
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Project Lombok is a Java library that helps reduce boilerplate code in your projects.

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.

@NoArgsConstructor, @RequiredArgsConstructor, and @AllArgsConstructor generate


constructors with no arguments, required arguments, and all arguments respectively.

@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).

How to configure it :- In intellij you just to add the plugin of lombok

Without Lombok:

public class User {


private Long id;
private String name;
private String email;

public User() {}

public User(Long id, String name, String email) {


this.id = id;
this.name = name;
this.email = email;
}

public Long getId() {


return id;
}

public void setId(Long id) {


this.id = id;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public String getEmail() {


return email;
}

public void setEmail(String email) {


this.email = email;
}

@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;
}

Adding Lombok to a Spring Boot Project:


<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>

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;

public class UserRegistrationDTO {

@NotBlank(message = "Name is mandatory")


private String name;

@Email(message = "Email should be valid")


@NotBlank(message = "Email is mandatory")
private String email;

@Min(value = 18, message = "Age should be at least 18")


private int age;

// Getters and Setters


}
HTTP status codes:-
HTTP status codes are standardized codes that indicate the result of an HTTP request (the
response is send by server and the logic for status code is written in Dispatcher Servlet). They
are categorized into five classes based on the type of response:

● 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.

Most common are:-


❖ 200 OK: The request was successful.
❖ 302 Found: The resource requested is temporarily located at a different URL.
❖ 400 Bad Request: The server cannot or will not process the request due to a client error
(e.g., malformed request syntax).
❖ 401 Unauthorized: The request requires user authentication.
❖ 403 Forbidden: The server understood the request but refuses to authorize it.
❖ 404 Not Found: The server cannot find the requested resource.
❖ 405 Method Not Allowed: The request method is not supported for the requested
resource.
❖ 408 Request Timeout: The server timed out waiting for the request.
❖ 500 Internal Server Error: The server encountered an unexpected condition that
prevented it from fulfilling the request.

All are here you can go through it in your free time:-

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.

4xx Client Error


400 Bad Request: The server cannot or will not process the request due to a client error (e.g.,
malformed request syntax).
401 Unauthorized: The request requires user authentication.
403 Forbidden: The server understood the request but refuses to authorize it.
404 Not Found: The server cannot find the requested resource.
405 Method Not Allowed: The request method is not supported for the requested resource.
408 Request Timeout: The server timed out waiting for the request.
409 Conflict: The request could not be completed due to a conflict with the current state of the
resource.
410 Gone: The resource requested is no longer available and will not be available again.
415 Unsupported Media Type: The request entity has a media type that the server or resource
does not support.
429 Too Many Requests: The user has sent too many requests in a given amount of time ("rate
limiting").

5xx Server Error


500 Internal Server Error: The server encountered an unexpected condition that prevented it
from fulfilling the request.
501 Not Implemented: The server does not support the functionality required to fulfill the
request.
502 Bad Gateway: The server, while acting as a gateway or proxy, received an invalid response
from the upstream server.
503 Service Unavailable: The server is currently unable to handle the request due to temporary
overload or maintenance.
504 Gateway Timeout: The server, while acting as a gateway or proxy, did not receive a timely
response from the upstream server.
505 HTTP Version Not Supported: The server does not support the HTTP protocol version used
in the 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:

### Client and Server Roles in HTTP Communication

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.

### Process of Sending HTTP Status Codes

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).

You might also like