What we saw in the first chapter was nothing but a glimpse of Spring MVC. Our total focus was just to get a Spring MVC application running. Now it's time for us to deep dive into the Spring MVC architecture.
By the end of this chapter, you will have a clear understanding of:
In the first chapter, we provided a little introduction to the Dispatcher servlet and you saw how to configure a Dispatcher servlet using the DispatcherServletInitializer
class. You learned that every web request first comes to the Dispatcher servlet. The Dispatcher servlet is the thing that decides which controller method the web request should be dispatched to. In the previous chapter, we created a welcome page that will be shown whenever we enter the URL http://localhost:8080/webstore/
in the browser. Mapping a URL to the appropriate controller method is the primary duty of the Dispatcher servlet.
So the Dispatcher servlet reads the web request URL and finds the appropriate controller method that can serve that web request and invokes it. This process of mapping a web request onto a specific controller method is called request mapping. And the Dispatcher servlet is able to do this with the help of the @RequestMapping
(org.springframework.web.bind.annotation.RequestMapping
) annotation.
Let's observe what will happen when we change the value
attribute of the @RequestMapping
annotation.
webstore
project; just right-click on your project and choose Run As | Run on Server. Now you will be able to see the same welcome message in the browser.http://localhost:8080/webstore/welcome
. WARNING: No mapping found for HTTP request with URI
[/webstore/welcome] in DispatcherServlet with name
'DefaultServlet'
HomeController
class and change the @RequestMapping
annotation's value
attribute to /welcome
and save it. Basically, your new request mapping annotation will look like as follows: @RequestMapping("/welcome")
.After starting our application, when we enter the URL http://localhost:8080/webstore/welcome
in the browser; the Dispatcher servlet (org.springframework.web.servlet.DispatcherServlet
) immediately tried to find a matching controller's method for the request path /welcome
.
In a Spring MVC application, a URL can be logically divided into five parts; see the figure that is present after this tip. The @RequestMapping
annotation only matches against the URL request path; it will omit the scheme, host name, application name, and so on. Here the application name is just a context name where the application is deployed—it is totally under the control of how we configure the web server.
The @RequestMapping
annotation has one more attribute called method
to further narrow down the mapping based on HTTP request method types (GET
, POST
, HEAD
, OPTIONS
, PUT
, DELETE
, TRACE
). If we do not specify the method
attribute in the @RequestMapping
annotation, the default method
would be GET
. You will see more about the method
attribute of the @RequestMapping
annotation in Chapter 4, Working with Spring Tag Libraries under Time for action – serving and processing forms.
But since we don't have a corresponding request mapping for the given URL path /welcome
, we are getting the HTTP status 404 error in the browser and the following error log in the console:
WARNING: No mapping found for HTTP request with URI [/webstore/welcome] in DispatcherServlet with name 'DefaultServlet'
From the error log, we can clearly understand that there is no request mapping for the URL path /webstore/welcome
. So we are trying to map this URL path to the existing controller's method; that's why in step 4 we only put the request path value /welcome
in the @RequestMapping
annotation as the value
attribute. Now everything works perfectly fine.
Suppose I have a Spring MVC application for library management called BookPedia and I want to map a web request URL http://localhost:8080/BookPedia/category/fiction
to a controller's method—how would you form the @RequestMapping
annotation?
@RequestMapping("/fiction")
@RequestMapping("/category/fiction")
@RequestMapping("/BookPedia/category/fiction")
What is the request path in the following URL http://localhost:8080/webstore/
?
webstore/
/
8080/webstore/