Spring Boot Defining @RequestMapping handler methods

@RequestMapping handler methods can have very flexible signatures.

In this article, we are going to take a look at what happens when you define a method that is annotated with @RequestMapping. @RequestMapping is one of the most widely used Spring MVC annotation. RequestMapping annotation is used to map web requests onto specific handler classes and/or handler methods. If you have done any work with Spring MVC you have undoubtedly come across this annotation before. 


The reason we are talking about this is that I had a student ask me the following question.

Hi Dan, I’m really enjoying the course so far but there are a few things that don’t quite fit to my head.

In the lesson 43 (Error Handling) we have CustomErrorController  class where we are injecting ErrorAttributes  inside the constructor via @AutoWired  so I assume that Spring boot will initialize that object automatically for us.

Now what about the error()  method? How did the parameters Model model and HttpServletRequest request  came up? Is spring boot going to initialize those objects for us too? (apparently yes cause they just work) But then why @AutoWired  is not needed to automatically inject those objects just like what happens in constructor? 

Furthermore how could I know that I need the above 2 parameters? Could there be any further parameters in the error method that I should be aware of? If yes would they be initialized automagically like Model  and HttpServletRequest ?


@RequestMapping Examples

Before we dive into the solution and explain why this happening I want to catch the rest of you up with what is going on. I had a custom error controller that I was using that looked something like this. 

As the student pointed out when we create the constructor and annotated it with @Autowired (No longer needed in Spring 4.3+) Spring managed the constructor injection of the managed bean ErrorAttributes. This is one of those beans that the Spring Framework manages for us. 

So now the question is I have this method called error and in this method, I have some method arguments, so how are those being supplied for me? 

Defining @RequestMapping handler methods

To understand the problem we must first understand that the Custom Error Controller I created is nothing special. In fact, we can create a regular controller and it would act the same. This is an example of a Home Controller I used to respond to the “/home” request. If you look closely now though, I have 2 different method parameters being passed to my controller. What the heck is going on here? 

The real secret to what is happening here is the @RequestMapping annotation on the method. If you look back to our CustomErrrorController you will see the same annotation on the method in question. If you take a look at the Spring MVC documentation on @RequestMapping handler Methods

@RequestMapping handler methods can have very flexible signatures. Most arguments can be used in arbitrary order with the only exception being BindingResult arguments.

What this means is that when we define a method and use the annotation @RequestMapping we have access to a number of method arguments that help us get information like;

  • Request & Response Objects
  • Web Request
  • Session Information
  • Http Method
  • Path Variables
  • Request Parameters
  • Request Body
  • Model
  • Errors
  • Binding Results
  • Much More…

If you want to see all of the available @RequestMapping handler methods check out the documentation here.


Spring does a lot of things behind the scenes for us and I completely understand how this can be confusing at times. You can usually find the answers in the documentation but at the same time, we don’t always know what to look for. I can assure you I don’t remember all of the method arguments and that is why I have that link above bookmarked. 

Question: Is there anything that happens behind the scenes in Spring you don’t quite understand?

Hi, I’m Dan Vega. I am a Software Engineer living just outside of one of my favorite places in the world, Cleveland, Ohio. I am a self-taught programmer who firmly believes that you can accomplish anything in this life if you put your mind to it, roll up your sleeves and are willing to put in the work.

I have been writing software for over 18+ years now and I use my knowledge, passion, and influence to help other developers achieve their goals. I teach online and currently, have over 30,000 students.

When I am not writing software, I have a number of things that really keep me busy. I love to read books, listen to podcasts and watch online courses. I have a real thirst for knowledge and it consumes me every single day of the week. I also love to run & lift heavy weights which keep me balanced throughout the week.

Please note: I reserve the right to delete comments that are offensive or off-topic.