DEV Community

Er. Bhupendra
Er. Bhupendra

Posted on

PART 11 :CONTROLLER ALL CONCEPT IN SPRINGBOOT PROJECT

Short answer: tum 80–85% core concepts cover kar chuke ho πŸ’ͺ
Lekin industry + interview ke point of view se kuch IMPORTANT cheezein abhi add karni chahiye.
Main unko checklist style me de raha hoon πŸ‘‡


βœ… 1️⃣ Validation (DTO level) β€” MOST IMPORTANT

Tum DTO bana rahe ho βœ”
Lekin validate karna bhi zaroori hai:

public class UserDTO {

    @NotBlank
    private String username;

    @Email
    private String email;

    @Size(min = 6)
    private String password;
}
Enter fullscreen mode Exit fullscreen mode

Controller:

@PostMapping
public ResponseEntity<UserDTO> create(@Valid @RequestBody UserDTO dto) {
    return ResponseEntity.ok(userService.save(dto));
}
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Interview line:

Validation always DTO par hoti hai, entity par nahi.


βœ… 2️⃣ Exception Handling (Service se throw, ControllerAdvice catch)

Service:

public User findById(Long id) {
    return userRepository.findById(id)
        .orElseThrow(() -> new ResourceNotFoundException("User not found"));
}
Enter fullscreen mode Exit fullscreen mode

Global handler:

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(ResourceNotFoundException.class)
    public ResponseEntity<String> handleNotFound(ResourceNotFoundException ex) {
        return ResponseEntity.status(HttpStatus.NOT_FOUND)
                .body(ex.getMessage());
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Rule:

Controller me try-catch ❌
GlobalExceptionHandler βœ…


βœ… 3️⃣ Don’t return Optional from Controller

❌ Wrong:

ResponseEntity<Optional<UserDTO>>
Enter fullscreen mode Exit fullscreen mode

βœ… Correct:

UserDTO dto = userService.findById(id);
return ResponseEntity.ok(dto);
Enter fullscreen mode Exit fullscreen mode

βœ… 4️⃣ Pagination + DTO (real world must)

Page<User> page = userRepository.findAll(pageable);

Page<UserDTO> dtoPage = page.map(userMapper::toDTO);

return ResponseEntity.ok(dtoPage);
Enter fullscreen mode Exit fullscreen mode

βœ… 5️⃣ Mapping best practice

Avoid:

new UserDTO(user);
Enter fullscreen mode Exit fullscreen mode

Prefer:

userMapper.toDTO(user);
Enter fullscreen mode Exit fullscreen mode

(Mapper class or MapStruct)


βœ… 6️⃣ HTTP Status codes properly use karo

Tum mostly ok() use kar rahe ho, but:

Case Status
Create 201 CREATED
Delete 204 NO_CONTENT
Not found 404
Validation fail 400
return ResponseEntity.status(HttpStatus.CREATED).body(dto);
Enter fullscreen mode Exit fullscreen mode

βœ… 7️⃣ Don’t expose Entity relationships directly

Bad:

User {
   List<Order> orders;
}
Enter fullscreen mode Exit fullscreen mode

DTO me:

UserDTO {
   List<OrderDTO> orders;
}
Enter fullscreen mode Exit fullscreen mode

Never:

ResponseEntity<User>   // with relations
Enter fullscreen mode Exit fullscreen mode

βœ… 8️⃣ Layer Responsibility (VERY IMPORTANT)

Layer Kya kare
Controller request/response only
Service business logic
Repository DB access
Mapper convert entity ↔ DTO

Interview question:

Can we inject repository directly in controller?
Answer:
❌ No, always through service layer.


βœ… 9️⃣ Wrapper Response (optional but pro)

ResponseEntity<ApiResponse<UserDTO>>
Enter fullscreen mode Exit fullscreen mode

Uniform response:

{
  "success": true,
  "data": {...},
  "message": "User fetched"
}
Enter fullscreen mode Exit fullscreen mode

βœ… 10️⃣ Security awareness (theory level)

You should know:

  • ❌ Never return password
  • ❌ Never expose internal IDs blindly
  • βœ” DTO hides fields
  • βœ” Use @JsonIgnore if needed

🎯 Final Honest Answer

You already know:
βœ” Generics
βœ” ResponseEntity
βœ” Entity vs DTO
βœ” Service layer
βœ” Mapping

You were missing mainly:
❗ Validation
❗ Exception handling
❗ Status codes
❗ Pagination mapping
❗ Layer responsibility
❗ Optional handling


🧠 Interview-ready one line:

I use DTOs for API, map entities using a mapper, validate DTOs, handle exceptions globally with @ControllerAdvice, and return proper HTTP status codes using ResponseEntity.


If you want, next I can give you a FULL REAL-WORLD FLOW:

Controller
  ↓
DTO
  ↓
Mapper
  ↓
Entity
  ↓
Repository
Enter fullscreen mode Exit fullscreen mode

with:
βœ” POST
βœ” GET
βœ” Exception
βœ” Validation
βœ” Pagination

Just say:
πŸ‘‰ "Give me full production-style flow example"

Top comments (0)