How Spring MVC Processes Requests: Core Components and Controller Variants

This article explains how Spring MVC handles incoming requests by detailing each core component—from DispatcherServlet to ViewResolver—and demonstrates various controller definitions, including @RestController, HttpRequestHandler, custom Controller, and servlet-based approaches, with code examples and configuration tips.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
How Spring MVC Processes Requests: Core Components and Controller Variants

Overview

When a request arrives, Spring MVC processes it through a series of core components.

Spring MVC processing flow

DispatcherServlet : entry point for all requests.

HandlerMapping : maps request URLs to handler objects.

HandlerAdapter : invokes the matched handler.

HandlerMethodArgumentResolver : resolves method arguments.

HandlerMethodReturnValueHandler : processes the return value.

ViewResolver : resolves the view when a ModelAndView is returned.

Standard @RestController definition

@RestController
@RequestMapping("/users")
public class UsersController {
  @GetMapping("/save")
  public Object save(Users users) {
    return users ;
  }
}

This is the most common way to define a controller in Spring MVC.

HttpRequestHandler implementation

@Component("/others/chrh")
public class ControllerHttpRequestHandler implements HttpRequestHandler {
  @Override
  public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html;charset=utf8");
    PrintWriter out = response.getWriter();
    out.print("<h1>你好,HttpRequestHandler</h1>");
    out.close();
  }
}

Using @Component with a leading '/' registers the bean with BeanNameUrlHandlerMapping.

Custom Controller implementing Controller interface

@Component("/others/custom")
public class CustomController implements Controller {

  @Override
  public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    response.setContentType("text/html;charset=utf8");
    PrintWriter out = response.getWriter();
    out.print("<h1>Controller接口</h1>");
    out.close();
    return null;
  }

}

The bean name also starts with '/' so BeanNameUrlHandlerMapping can map it.

Servlet‑based controller extending HttpServlet

@Component("/others/servlet")
public class ControllerServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;

  @Override
  protected void service(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html;charset=utf8");
    PrintWriter out = response.getWriter();
    out.print("<h1>你好 HttpServlet</h1>");
    out.close();
  }
}

To make this work you must also register a SimpleServletHandlerAdapter bean.

@Configuration
public class WebConfig implements WebMvcConfigurer {
  @Bean
  public SimpleServletHandlerAdapter simpleServletHandlerAdapter() {
    return new SimpleServletHandlerAdapter();
  }
}

Optionally, you can use @WebServlet and enable @ServletComponentScan for servlet registration.

The article concludes that these are the main ways Spring MVC supports controller definitions, with a promise of deeper source‑code analysis in the next post.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

javaControllerSpring MVCDispatcherServletHandlerMapping
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.