Hello everyone, I am the second brother!

Learning Java, naturally not Spring Boot learning! As a Java technology blogger, I’ve been urged on the Spring Boot tutorial dozens of times, and it’s a bit awkward not to update it.

So I’m going to be intermittently Posting Spring Boot articles over the next few days, including some very entry-level tutorials and some very good Spring Boot project recommendations that seasoned starter’s should ignore.

“The Path to Java Programmer Advancement” has gained 872 stars on GitHub. If you haven’t lit up yet, click on it and let your brother experience the joy of 1K star. After all, there are still 128, not far away.

Github.com/itwanger/to…

I. Spring Boot project construction

Spring provides Spring Initializr as a way to create a Spring Boot project. The website is as follows:

start.spring.io/

The interface is as follows:

Think of Spring Initializr as an initialization wizard for a Spring Boot project that helps developers create a Spring Boot skeleton in less than a minute, foolishly.

To explain the key options in the Spring Initializr initialization interface.

1) Project: The construction method of the Project can be Maven or Gradle (build scripts are written based on languages like Groovy or Kotlin instead of traditional XML). The default is Maven.

2) Language: The development Language of the project, including Java, Kotlin (a programming Language developed by JetBrains that runs on the JVM), Groovy (which can be used as a scripting Language for the Java platform). The default is Java.

3) Spring Boot: The version of Spring Boot used by the project. The default version is ok, relatively stable.

4) Project Metada: basic Settings of the Project, including package name, packaging method, JDK version, etc.

  • Group: identifier of the organization to which the project belongs, for example viP.r2java;
  • Artifact: an identifier for a project, such as tobebetterjavaer;
  • Name: Retain the Artifact as the default;
  • Description: A Description of the project, such as The Path to Java Programmer Advancement.
  • Package name: The project Package name, which is automatically generated based on groups and artifacts.
  • Packaging: The project packaging method can be Jar or War (in SSM era, JavaWeb projects are usually classified into War package and placed under Tomcat). In Spring Boot era, the default Jar package is ok. Tomcat, Jetty, Undertow and other service containers can be built into Spring Boot.
  • Java: The JDK version of the project, either 11 or 8.

5) Dependencies: Project Dependencies and starter If this parameter is not selected, only the core module spring-boot-starter and the test module spring-boot-starter-test are available by default.

Ok, next we use Spring Initializr to initialize a Web Project, Project select Maven, Spring Boot select 2.6.1, Java select JDK 8, Dependencies Select “Build Web, including RESTful, Applications using Spring MVC. Uses Apache Tomcat as the default Embedded Container.”

This indicates that we will adopt SpringMVC and use Tomcat as the default server to develop a Web project.

Then click the “Generate” button at the bottom to generate a compressed package of the Spring Boot initialization project.

Ii. Structural analysis of Spring Boot project

Unzip the package and import it into Intellij IDEA to see the directory structure of the Spring Boot project.

You can use the tree-cfl 3 command to list the contents of a directory in a tree:

  • SRC /main/ Java is the development directory of the project, where the business code is written.
  • SRC /main/resources is the configuration file directory, where static files, template files, and configuration files are stored.
    • The static subdirectory is used to store static resource files, such as JS and CSS images.
    • The templates subdirectory is used to hold template files, such as thymeleaf and freemarker files.
  • SRC /test/ Java is the test class file directory.
  • Pom.xml is used to manage project dependencies and builds.

3. Start the Spring Boot project

For the first time, I personally used to right-click in the main class and choose “Run… main()” from the pop-up right-click menu to start.

After about 2.5s build, the project started successfully. You can see from the log that the Web project uses Tomcat as the container, the default port number is 8080, and the root path is empty.

This is easier than traditional Web projects. There is no need to create a WAR package and put the WAR package into the Tomcat webApp directory before starting.

If you want to jar your project to the server and run it as java-jar xxx.jar, what should you do?

Start the Terminal, run the MVN clean package command, and wait for the package result.

Our project was initialized with a Maven build, so the pop.xml file introduces the spring-boot-Maven-plugin.

<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId>  </plugin> </plugins> </build>Copy the code

So we can use the Maven command to complete the packaging of the project, after the packaging is completed, go to the target directory, you can see the packaged JAR package.

Using the terminal tool Tabby, upload the JAR package to the server.

Run the Java -jar tobeBetterJavaer -0.0.1 -snapshot. jar command.

What ?????? There is no JDK installed. Well, in order to learn Linux with the friends of Aliyun server, I spent a lot of money to buy a server with zero addition.

PS: you need to install JDK in centos environment.

Segmentfault.com/a/119000001…

Once the JDK is installed, execute the command again and you can see that the Spring Boot project is running normally on the server.

4. Develop the first Spring Boot project

Now that the project has started successfully, let’s test it by accessing port 8080 in the browser.

This 404 page is a Spring Boot default error page, indicating that our request does not exist in the Web service.

So what to do?

Let’s add a Controller file to handle Web requests as follows.

@Controller public class HelloController { @GetMapping("/hello") @ResponseBody public String hello() { return "hello, springboot"; }}Copy the code

The business logic of this code is very simple. The user sends a Hello request, and the server responds with a “Hello, Springboot”.

OK, it is now accessible. This means that our first Spring Boot project is complete.