🎯 My Spring Boot Controllers Looked Fine… Until I Understood the Presentation Layer
Source: Dev.to
🧠 What is the Presentation Layer (in simple terms)?
The Presentation Layer is the entry point of your Spring Boot application. It is responsible for:
- Receiving HTTP requests from the client
- Mapping requests to the correct endpoints
- Accepting and converting input data
- Returning responses in JSON/XML
It should not contain business logic.
It should not talk directly to the database.
Its job is simple:
Handle requests → delegate work → return responses
🏗️ The Big Picture: Spring Boot Project Structure
A clean Spring Boot web application usually looks like this:
client
controller
service
dto
repository
entity
database
Key rule:
👉 Controllers talk to Services, not to Databases.
Once I followed this, my code became cleaner overnight.
🎮 Controllers: The Heart of the Presentation Layer
Spring MVC gives us an annotation‑based programming model to build controllers.
Important annotations
@Controller– used in traditional MVC applications that return views.@RestController– a shortcut for@Controller + @ResponseBody. Every method inside a@RestControllerdirectly returns JSON/XML to the client. For REST APIs,@RestControlleris the default choice.
🔗 Request Mapping: How Requests Find Your Code
Spring needs to know which URL maps to which method. That’s where request mappings come in.
@RequestMapping
A generic mapping that supports:
- URL paths
- HTTP methods
- Headers
- Media types
In real projects we mostly use its cleaner alternatives.
🚦 HTTP Method‑Specific Mappings (Use These)
Spring provides shortcut annotations that make APIs readable:
@GetMapping→ fetch data@PostMapping→ create data@PutMapping→ update data@DeleteMapping→ delete data@PatchMapping→ partial update
Using these properly makes APIs self‑explanatory.
🌍 Dynamic URLs: @PathVariable vs @RequestParam
@PathVariable
/employees/123
Use this when the value is mandatory and uniquely identifies a resource.
@RequestParam
/employees?id=123
Use this when the parameter is optional, e.g., for filtering, sorting, pagination.
Understanding this difference improves API design instantly.
📦 Handling Input Data with @RequestBody
When a client sends data (JSON/XML), Spring converts it into a Java object via @RequestBody. It is commonly used in:
- POST
- PUT
- PATCH
Spring’s message converters (e.g., Jackson) automatically transform JSON into Java objects.
⚠️ The Mistake I Was Making
I used to:
- Put logic inside controllers
- Validate data manually
- Call repositories directly
That broke the separation of concerns. The Presentation Layer is not for business logic. Respecting this boundary makes APIs:
- Cleaner
- Easier to test
- Easier to maintain
🚀 Final Thoughts
The Presentation Layer may look simple, but it decides how clean your backend becomes.
If controllers are clean:
- Services shine
- Bugs reduce
- Scaling becomes easier
Understanding this layer was a turning point in my Spring Boot journey.
This post is part of my learning‑in‑public journey while exploring Spring Boot and backend development.