Backend Development 5 min read

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

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

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

HttpRequestHandler implementation

<code>@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();
  }
}
</code>

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

Custom Controller implementing Controller interface

<code>@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;
  }

}
</code>

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

Servlet‑based controller extending HttpServlet

<code>@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();
  }
}
</code>

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

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

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.

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

login 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.