<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>net.codejava</groupId> <artifactId>spring-boot-hello-world</artifactId> <version>1.0.0</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>Here, you can see the parent of this POM is spring-boot-starter-parent which defines common defaults such as Java version (1.8), UTF-8 source encoding, various Maven plug-in configurations for Java Spring framework development, resource filtering… It also inherits dependency management from spring-boot-dependencies which defines dependency information for Spring Boot, Spring framework and common libraries.The dependency spring-boot-starter-web is a Spring Boot Starter that specifies a group of dependencies for developing Java web applications such as Spring Web MVC, and Embed Tomcat server.And the plug-in spring-boot-maven-plugin enables automatic creation of an executable JAR file for a stand-alone Java web application with embedded Tomcat server, thanks to its nested jars support.You can see the dependency information of the project by typing the following Maven’s command:
mvn -dependency:tree
package net.codejava; import org.springframework.boot.*; import org.springframework.boot.autoconfigure.*; import org.springframework.web.bind.annotation.*; @RestController @EnableAutoConfiguration public class SpringBootHelloWorld { @RequestMapping("/") String home() { return "Hello World Spring Boot!"; } public static void main(String[] args) throws Exception { SpringApplication.run(SpringBootHelloWorld.class, args); } }Here, the @RestController annotation tells Spring that this class is a web controller and the controller methods should return the resulting string directly back to the client in REST style.The @EnableAutoConfiguration annotation tells Spring Boot to automatically configure the application based on the specified dependencies. In this simple web application, Spring Boot configures Spring MVC and embedded Tomcat.And perhaps you are very familiar with the @RequestMapping annotation, which provides routing information for Spring Dispatcher Servlet. In our application, it specifies any HTTP requests with the/path should be handled by the home() method.In the home() method, it simply returns a string to the client.In the main() method, we call SpringApplication.run() to bootstrap our web application running in the embedded Tomcat web server. Spring framework configures the specified class (SpringBootHelloWorld) as a web component. The String array args is passed through the run() method to support configuration via application’s command-line arguments when needed.
mvn spring-boot:run
The goal spring-boot:run tells Maven to compile the project and launch Spring Boot. If the compilation is successful, you can see the logo of Spring Boot appears like this:Spot the logging information, you can see the embedded Tomcat server is started to listen on port 8080 and our web application is deployed - ready to accept client’s requests:Now you can type the following URL in your web browser to access the web application:http://localhost:8080/
And the result looks like this:You see, the browser displays “Hello World Spring Boot!” which is exactly what returned from the home() method.Press Ctrl + C to stop the server and exit Spring Boot application.mvn package
Then you can see the maven-jar-plugin creates the executable JAR file in the target directory:Check the target directory, and you can see the spring-boot-hello-world-1.0.0.jar - this is our web application with an embedded Tomcat server - everything is packed in a single executable JAR file (around 16 MB).Related videos:java -jar target\spring-boot-hello-world-1.0.0.jar
Then you see Spring Boot logo appears:This Spring Boot application is actually a web server.