C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Spring Boot Starter WebThere are two important features of spring-boot-starter-web:
If we want to develop a web application, we need to add the following dependency in pom.xml file: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.2.2.RELEASE</version> </dependency> Starter of Spring web uses Spring MVC, REST and Tomcat as a default embedded server. The single spring-boot-starter-web dependency transitively pulls in all dependencies related to web development. It also reduces the build dependency count. The spring-boot-starter-web transitively depends on the following:
By default, the spring-boot-starter-web contains the following tomcat server dependency: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <version>2.0.0.RELEASE</version> <scope>compile</scope> </dependency> The spring-boot-starter-web auto-configures the following things that are required for the web development:
Spring Boot Embedded Web ServerEach Spring Boot application includes an embedded server. Embedded server is embedded as a part of deployable application. The advantage of embedded server is, we do not require pre-installed server in the environment. With Spring Boot, default embedded server is Tomcat. Spring Boot also supports another two embedded servers:
Using another embedded web serverFor servlet stack applications, the spring-boot-starter-web includes Tomcat by including spring-boot-starter-tomcat, but we can use spring-boot-starter-jetty or spring-boot-starter-undertow instead. For reactive stack applications, the spring-boot-starter-webflux includes Reactor Netty by including spring-boot-starter-reactor-netty, but we can use spring-boot-starter-tomcat, spring-boot-starter-jetty, or spring-boot-starter-undertow instead. Jetty ServerThe Spring Boot also supports an embedded server called Jetty Server. It is an HTTP server and Servlet container that has the capability of serving static and dynamic content. It is used when machine to machine communication is required. If we want to add the Jetty server in the application, we need to add the spring-boot-starter-jetty dependency in our pom.xml file. Remember: While using Jetty server in the application, make sure that the default Tomcat server is excluded from the spring-boot-starter-web. It avoids the conflict between servers. <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> We can also customize the behavior of the Jetty server by using the application.properties file. Undertow ServerSpring Boot provides another server called Undertow. It is also an embedded web server like Jetty. It is written in Java and manage and sponsored by JBoss. The main advantages of Undertow server are:
Remember: While using Undertow server in the application, make sure that the default Tomcat server is excluded from the spring-boot-starter-web. It avoids the conflict between servers. <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-undertow</artifactId> </dependency> We can also customize the behavior of the Undertow server by using the application.properties file. spring-boot-starter-web vs. spring-boot-starter-tomcatThe spring-boot-starter-web contains the spring web dependencies that includes spring-boot-starter-tomcat. The spring-boot-starter-web contains the following:
While the spring-boot-starter-tomcat contains everything related to Tomcat server.
The starter-tomcat has the following dependencies: <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-core</artifactId> <version>8.5.23</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-el</artifactId> <version>8.5.23</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-websocket</artifactId> <version>8.5.23</version> <scope>compile</scope> </dependency> We can also use spring-mvc without using the embedded Tomcat server. If we want to do so, we need to exclude the Tomcat server by using the <exclusion> tag, as shown in the following code. <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency>
Next TopicSpring Data JPA
|