Skip to content
intermediatePhase ·

@Controller

Use @Controller for traditional MVC controllers.

25m
0 problems
Topic Progress0%

@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

  1. Validation: Always validate input at the boundary
  2. Error Handling: Use structured error responses
  3. Logging: Log key events for debugging
  4. Testing: Unit, integration, and load tests
  5. Documentation: Keep docs updated with code changes

Practice Problems

0/3solved
Implement @Controller Annotation

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
}
@Controller Annotation Edge Cases

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, idempotency
@Controller Annotation Testing Strategy

Write 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 injection

Quiz

1. What does @Controller return by default?

Question 1 options

2. What is the difference between @Controller and @RestController?

Question 2 options

3. What is the primary purpose of @Controller Annotation?

Question 3 options

4. What is a common mistake when implementing @Controller Annotation?

Question 4 options

Flashcards

Question

@Controller default return type?

Answer

View name (String) — resolved by ViewResolver

Question

@RestController vs @Controller?

Answer

@RestController = @Controller + @ResponseBody (returns JSON)

Question

What is @Controller Annotation?

Answer

@Controller Annotation is a key concept in backend development.

Question

When to use @Controller Annotation?

Answer

Use @Controller Annotation when building production systems that require reliability, scalability, and maintainability.

Question

@Controller Annotation best practices

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