Went to get the third dog bite injection and also do not feel much energetic
So where were we ?
So let’s just start with a Web Server
Step 1 : How to make a Hello World in Java Step 2 : Add Logger in Java Step 3 : Add Request Validator in Java
Once these Done we can think of going to the next steps
Link to original
- Project Aim
- Procedure
- Result
- So let’s make the logger today Add Logger in Java : Done
- Before solving Add a request Validator It’s better to make a post request which takes a username and password and return them.
First Java Post Request
How to Take request in Post Controller
So apparently This is a little different that what we see in Nodejs
- This is called the DTO Pattern Data Transfer Object
// Request DTO: only what client sends
public class LoginRequestDTO {
private String username;
private String password;
// Getters and setters
public String getUsername() { return username; }
public void setUsername(String username) { this.username = username; }
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }
}
public class LoginResponseDTO {
private String username;
// Constructor
public LoginResponseDTO(String username) {
this.username = username;
}
// Getter
public String getUsername() { return username; }
}Now what if there are a specific type
{
user : {
name: {
first_name : "John",
"last_name" : "doe"
}
}
}
}public class UserRequestDTO {
private User user;
// getters and setters
public User getUser() { return user; }
public void setUser(User user) { this.user = user; }
public static class User {
private Name name;
// getters and setters
public Name getName() { return name; }
public void setName(Name name) { this.name = name; }
}
public static class Name {
private String first_name;
private String last_name;
// getters and setters
public String getFirst_name() { return first_name; }
public void setFirst_name(String first_name) { this.first_name = first_name; }
public String getLast_name() { return last_name; }
public void setLast_name(String last_name) { this.last_name = last_name; }
}
}
sequenceDiagram participant Client participant Controller participant LoginRequestDTO participant LoginResponseDTO Client->>Controller: POST / with JSON { username, password } Controller->>LoginRequestDTO: Map JSON to LoginRequestDTO Controller->>Controller: Process login data Controller->>LoginResponseDTO: Create LoginResponseDTO with username only Controller->>Client: Return JSON { username }
Explanation of the DTOs in this flow:
-
LoginRequestDTO:
-
Represents the data sent from the client to the server.
-
Contains
usernameandpasswordfields. -
Used by Spring to deserialize the incoming JSON request body into a Java object.
-
Helps the controller safely receive only the expected data.
-
-
LoginResponseDTO:
-
Represents the data sent from the server back to the client.
-
In this case, contains only the
username. -
Used by Spring to serialize the Java object back into JSON for the HTTP response.
-
Helps control exactly what data is exposed to the client (e.g., hiding password).
-
Why use separate DTOs?
-
Security: You avoid sending sensitive information like passwords back to the client.
-
Clear API contract: You define exactly what your API expects and what it returns.
-
Decoupling: Allows your internal domain or entity models to change without affecting API contracts.