@Controller
@Controller — Web Layer
@Controller
@RequestMapping("/products")
public class ProductController {
private final ProductService productService;
public ProductController(ProductService productService) {
this.productService = productService;
}
@GetMapping
public String listProducts(Model model) {
model.addAttribute("products", productService.getAll());
return "product-list"; // Returns view name
}
@GetMapping("/{id}")
public String getProduct(@PathVariable Long id, Model model) {
model.addAttribute("product", productService.getById(id));
return "product-detail";
}
}
@Controller vs @RestController
| Aspect | @Controller | @RestController |
|---|---|---|
| Return type | View name (String) | ResponseBody (JSON) |
| Use case | Server-rendered HTML | REST API |
| Annotation | Needs @ResponseBody | Built-in @ResponseBody |
@Controller with @ResponseBody
@Controller
@RequestMapping("/api/products")
public class ProductController {
@ResponseBody // Return JSON, not view
@GetMapping("/{id}")
public Product getProduct(@PathVariable Long id) {
return productService.getById(id);
}
}
Spring Best Practices
Configuration
- Use properties over YAML for simple configs
- Externalize configuration
- Use profiles for environments
- Validate on startup
Bean Management
- Prefer constructor injection
- Use appropriate scope
- Implement lazy initialization
- Clean up resources
Security
- Use Spring Security
- Implement CSRF protection
- Use method-level security
- Log security events
Key Points
- Understanding @Controller Annotation is essential for production systems
- Always consider scalability and maintainability
- Test thoroughly before deploying to production
- Monitor performance and set up alerting
Common Patterns
- Validation: Always validate input at the boundary
- Error Handling: Use structured error responses
- Logging: Log key events for debugging
- Testing: Unit, integration, and load tests
- Documentation: Keep docs updated with code changes
Practice Problems
Design and implement a solution for @Controller Annotation in a backend system. Consider scalability, error handling, and production readiness.
Solution
// @Controller Annotation implementation
// Key aspects: validation, error handling, logging, testing
public class ControllerAnnotation {
// Production-ready implementation
}Identify and handle edge cases for @Controller Annotation. What happens under high load, with invalid input, or during failures?
Solution
// Edge case handling:
// 1. Null/empty input -> validation
// 2. High load -> rate limiting, queuing
// 3. Failures -> retries, circuit breaker
// 4. Concurrent access -> locks, idempotencyWrite a testing strategy for @Controller Annotation. Include unit tests, integration tests, and performance tests.
Solution
// Test plan:
// - Unit: 80% coverage target
// - Integration: API contracts
// - Performance: latency, throughput
// - Chaos: failure injectionQuiz
1. What does @Controller return by default?
2. What is the difference between @Controller and @RestController?
3. What is the primary purpose of @Controller Annotation?
4. What is a common mistake when implementing @Controller Annotation?
Flashcards
Question
@Controller default return type?
Click to reveal answer
Answer
View name (String) — resolved by ViewResolver
Question
@RestController vs @Controller?
Click to reveal answer
Answer
@RestController = @Controller + @ResponseBody (returns JSON)
Question
What is @Controller Annotation?
Click to reveal answer
Answer
@Controller Annotation is a key concept in backend development.
Question
When to use @Controller Annotation?
Click to reveal answer
Answer
Use @Controller Annotation when building production systems that require reliability, scalability, and maintainability.
Question
@Controller Annotation best practices
Click to reveal answer
Answer
Follow SOLID principles, write clean code, test thoroughly, document decisions, and monitor in production.
Revision Notes
Key Takeaways
- 1.@Controller handles HTTP requests in Spring MVC
- 2.Default return is view name for template rendering
- 3.@RestController = @Controller + @ResponseBody
- 4.Use @Controller for server-rendered, @RestController for APIs
Interview Tips
- •Know the difference between @Controller and @RestController
- •Understand view resolution
Cheat Sheet
@Controller
- @Controller: Returns view name (HTML templates)
- @RestController: Returns JSON (@Controller + @ResponseBody)
- @RequestMapping: Maps URL to controller
- @GetMapping/@PostMapping: Maps HTTP methods