Skip to content

harman-04/spring-mvc-request-mapping-basics

Repository files navigation

Spring MVC: Exploring @RequestMapping (Class & Method Levels)

Project Overview

This project focuses on the core routing mechanism of Spring MVC: the @RequestMapping annotation. It demonstrates how to create a hierarchical URL structure by applying mappings at both the Class level and the Method level. This structure helps in organizing controllers based on specific modules (e.g., /boys/hello, /boys/springmvc).


Technical Specifications & Concepts

1. The DispatcherServlet (Front Controller)

Configured in web.xml, the DispatcherServlet acts as the single entry point. By mapping it to /, it intercepts every request and delegates it to the appropriate controller based on the @RequestMapping definitions.

2. Request Mapping Hierarchy

This project illustrates a two-tier mapping strategy:

  • Class-Level Mapping: @RequestMapping("/boys") on the DemoController class serves as a base path. All methods inside this class are relative to this path.
  • Method-Level Mapping: @RequestMapping("/hello") inside the class defines the specific endpoint.

Resulting URL Logic: Base Context Path + Class Mapping + Method Mapping Example: http://localhost:8080/app/boys/hello

3. @ResponseBody

Since this project does not use a ViewResolver (like JSP), the @ResponseBody annotation is used. It tells Spring to bind the return value of the method directly to the HTTP response body, bypassing the usual view resolution logic.


File & Class Reference

pom.xml

  • Spring Web MVC (6.1.13): The framework dependency for web support.
  • Jakarta Servlet API (6.0.0): Required for compatibility with Tomcat 11.

web.xml (Deployment Descriptor)

Purpose: Defines the DispatcherServlet.

  • <servlet-name>: frontcontroller-dispatcher.
  • Matching Configuration: By convention, Spring looks for a file named frontcontroller-dispatcher-servlet.xml to load the application context.

frontcontroller-dispatcher-servlet.xml

Purpose: Spring configuration.

  • <context:component-scan>: Specifically looks into com.student.controllers to find the @Controller bean and register it in the Spring Container.

DemoController.java

Purpose: Routing and Business Logic.

  • Controller Logic:
    • If you hit /boys/hello, you get "Hello World".
    • If you hit /boys/springmvc, you get the welcome message.

Execution Lifecycle

  1. Server Startup: Tomcat 11 loads the web.xml and initializes the DispatcherServlet.
  2. Context Loading: Spring scans the com.student.controllers package.
  3. Request Handling:
    • User visits /boys/hello.
    • HandlerMapping identifies DemoController as the handler.
    • HandlerAdapter executes the helloWorld() method.
  4. Response: The String is written directly to the browser because of @ResponseBody.

Dependencies & Environment

  • Java Version: Compatible with Java 17/21/25.
  • Server: SmartTomcat 11 (Supports Jakarta EE 10/11).
  • Packaging: war (Web Application Archive).

How to Test

URLs to Access:

  1. Endpoint 1: http://localhost:8080/[context-path]/boys/hello
    • Output: Hello World
  2. Endpoint 2: http://localhost:8080/[context-path]/boys/springmvc
    • Output: Welcome to Spring mvc (Request Mapping Annotation)

About

A tutorial project demonstrating Class-level and Method-level URL routing using the @RequestMapping annotation in Spring MVC 6.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages