Table of contents
1.
Introduction📜
2.
Spring Boot Auto Configuration💡
3.
Dispatcher Servlet💡
4.
Frequently Asked Questions
4.1.
What does spring boot's @RestController annotation mean?
4.2.
Can @component take the place of @controller?
4.3.
Can we build a resource using Put rather than POST?
4.4.
What is the benefit of spring boot?
4.5.
Why is REST architecture important?
5.
Conclusion
Last Updated: Mar 27, 2024

Spring Boot Auto Configuration and Dispatcher Servlet

Author soham Medewar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction📜

Spring Boot is an open-source framework. It is based on the Java programming language. Spring Boot is used in creating a microservice. Spring Boot is developed by Pivotal Team. It can be used to build standalone spring applications. In this article, we will be building a simple RESTful web service project.

Spring Boot

In this article, we will discuss the working of the dispatcher servlet. Also, will see who configures the dispatcher servlet, the work of the dispatcher servlet, etc.

Spring Boot Auto Configuration💡

⚙️Spring Boot automatically configures the spring application. Configuration is done based on the dependencies present in the classpath.

⚙️Auto configuration in Spring Boot makes development faster and easier. You don't need to define certain beans that are included in auto-configuration.

⚙️Classic MVC database-driven Spring Boot application requires a lot of configuration. It needs a view resolver, dispatcher servlet, transaction manager, data source, Jackson, etc.

⚒️If the Spring MVC jar is on the classpath, then the dispatcher servlet is automatically configured by Spring Boot. The same goes for Jackson and Data Source.

⚙️With the help of @SpringBootApplication or @EnableAutoConfiguration annotation in the startup class, we can enable auto-configuration in Spring Boot.

⚙️It enables components scan. In any package and subpackage for any bean file, Spring will automatically start scanning classes. 

⚙️Examples of auto-configuration done by Spring Boot are given below:

⚒️DataSourceAutoConfiguration

⚒️DispatcherServletAutoConfiguration

⚒️ErrorMcvAutoConfiguration

⚒️JacksonAutoConfiguration

⚙️Auto-configuration done by Spring Boot can be seen in AUTO-CONFIGURATION or CONDITIONS EVALUATION REPORT.

⚙️By adding the following line in the code base, we can exclude classes from auto-configuration.

⚒️@SpringBootApplication (exclude={JacksonAutoConfiguration.class, JmxAutoConfiguration.class})  

⚒️Or we can add the following code in the application.properties file.
spring.autoconfiguration.exclude=org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration  

⚒️By removing unnecessary classes from auto-configuration, we can have a faster startup and better performance.

⚙️To generate the AUTO-CONFIGURATION report of debug mode, add the following code:

⚒️logging.level.org.springframework=debug  

           ⚙️To display Negative matches, Positive matches, Exclusions, and Unconditional classes under the CONDITIONS EVALUATION REPORT, run the RestfulWebServiceApplication.java file. It contains lots of information. 

Dispatcher Servlet💡

In Spring MVC, a single servlet known as the Dispatcher Servlet handles all incoming requests (front controller). A design pattern used in the creation of web applications is the front controller. All requests are received by a single servlet, which then distributes them to every other part of the program.

Dispatcher servlet

DispatcherServlet's responsibility is to accept an incoming URI and identify the ideal set of handlers (Controller classes) and views (usually JSPs). The DispatcherServlet renders the view as the response has been determined. The Response Object is finally returned to the client via the DispatcherServlet. The Dispatcher Servlet, in other words, plays an essential role.

One more thing that should be noticed is ErrorMvcAutoConfiguration: 

ErrorMvcAutoConfiguration matched:  
-@ConditionalOnClass found required classes 'javax.servlet.Servlet', 'org.springframework.web.servlet.DispatcherServlet' (OnClassCondition)- found 'session' scope (OnWebApplicatiossssnCondition)


It configures the errorAttributesbasicErrorControllerDefaultErrorViewResolverConfiguration, and ErrorMvcAutoConfiguration. It created Whitelabel Error Page, also called a default error page.

One more thing that is auto-configured is HttpMessageConvertersAutoConfiguration. These are message converters that automatically convert the message.

HttpMessageConvertersAutoConfiguration matched:  
-@ConditionalOnClass found required class 'org.springframework.http.converter.HttpMessageConverter' (OnClassCondition)  
----------------  
-----------------  
JacksonAutoConfiguration.Jackson2ObjectMapperBuilderCu


It initializes the message converter and Jackson bean. The conversion of JSON to bean and bean to JSON is done with the help of Jackson2ObjectMapper.

Must Read Spring MVC Flow and, Spring Tool Suite.

Frequently Asked Questions

What does spring boot's @RestController annotation mean?

The @Controller and @ResponseBody annotations are already present in the Spring RestController annotation, which is a convenience annotation. Applying this annotation to a class designates it as a request handler. The Spring MVC annotation, Spring RestController, is used to build RESTful web services.

Can @component take the place of @controller?

There is no difference between @Component@Service@Controller, and @Repository. The generic annotation @Component is used to represent the component of our MVC.

Can we build a resource using Put rather than POST?

You can build or edit a resource using either POST or PUT, as both can be used to submit data. Because PUT is idempotent, it is preferred by many web developers for creating a resource on the server. No matter how often you call PUT, the resource's condition won't be in danger.

What is the benefit of spring boot?

It greatly cuts down on development time and boosts output. It prevents the need to create a tonne of redundant XML configuration, annotations, and code. The Spring Ecosystem, which includes Spring JDBC, Spring ORM, Spring Data, Spring Security, etc., is very simple to integrate with the Spring Boot Application.

Why is REST architecture important?

REST allows us to achieve the architectural properties of performance, scalability, generality, simplicity, modifiability, and extensibility.

You can also read What is Servlet here.

Conclusion

In this article, we have extensively discussed auto-configuration and dispatcher servlet in Spring Boot.

If you want to learn more, check out our articles on STS Download, Implementing DELETE Method to Delete a User ResourceTechnological Services in Ready APIWhat Is Web2Py?Why To Use Web2py?Postbacks and Internationalization in web2pyThird Party Modules In Web2pyTasks In Web2py, and  XML in Web2py.

You can also consider our Spring Boot Course to give your career an edge over others.

Happy Learning!

Live masterclass