Introduction to the

For Java developers, where the Spring framework has been popular for years, everyone in Java development has embraced the Spring framework as an integral part of their development. The traditional pattern has been in the form of an applicationContext.xml configuration file, and for most entry-level developers, a single configuration error can result in a failure or worse, wasting valuable developer time and costing the company. The Spring open source organization has long been aware of this problem, and they have internalized the entire family of components of the Spring framework. It is just a matter of providing maven (JAR management, project package tool) or gradle (new JAR management, project package tool) to introduce parent-pom (Maven configuration file) or parent-gradle (gradle configuration file). Having every Spring project run as a subproject of Spring allows developers to focus on business logic and deeper architecture instead of having to worry about configuration files. Since then, SpringBoot has been born. It has a pure open source pedigree, and thanks to the Spring open source organization, we Java developers have brought convenience!

Main advantages of SpringBoot:

1. Faster start for all Spring developers 2. Out of the box, provide a variety of default configurations to simplify project configuration 3. Embedded containers simplify Web projects 4. No redundant code generation and XML configuration requirements 5. Provide a series of functional features for large enterprise projects (such as: security, health detection, external configuration, database access, restful build, many features are not described here, will be updated in later articles)

This ZhangMu

Use SpringBoot to complete a simple Web (SpringMVC) application, via @restController output “HelloWord” interface, let us experience the rapid development of SpringBoot, simple features.

System requirements

2. Springboot 1.5.2.RELEASE 3. Development tools (IntelliJ IDEA 2017.1.1) 4. Database Management Tools (Navicat Premium 11.1.7) 5. 6. Apache Maven (3.3.9, this chapter adopts the form of Maven jar package management, the specific configuration of environment variables and use please find information by yourself)

Start building the project

There are several ways to build projects right now and I’ll briefly cover two of them. 2. IntelliJ IDEA development tool to build the project structure

Build using SPRING INITIALIZR

Visit start.spring. IO/to see the following ‘Figure 1’ interface: The main parts of the interface include: administrative tools, Springboot versions, package names, artifacts, selected modules (I have selected a Web module).




Figure 1

Click Generate Project to download a zip file that creates the source code for the Project’s engineering architecture based on the information you entered. C :/lessions c :/lessions C :/lessions D :/lessions




Figure 2

After decompression, we can import it into the project through the IntelliJ IDEA development tool. 1. Choose File — >New — >Project from Existing Sources… 3. Click Import Project from External Model and select Maven. Click Next to finish. 4. If your environment has multiple JDK versions, please select System Install 1.8 when selecting Java SDK

Build using IntelliJ IDEA

Open the tool, and the interface is shown as “Figure 3” below:




Figure 3

We click Create New Project to Create a New SpringBoot application, as shown in Figure 4 below:




Figure 4.

We click Next to continue the Next operation, and fill in the corresponding content according to the interface input options, as shown in “Figure 5” :




Figure 5

Click Next to create the project, and it will be automatically imported into the project, so there is no need to import again. This way is easier, so we will use the second form to create a new project in our articles.

In the next step, we added the Web dependency to our project, as shown in the picture below:





The figure below

One thing to note here is that if we chose the WAR form above, also added the Web dependency and started it as Application during development, we would need to comment out the Scope property of the Tomcat dependency in the POM.xml configuration file.

Project directory structure

Let’s look at the directory structure that the tool generates for our new project, as shown in “Figure 6” :




Figure 6.

We can see that the directory structure has the following:

/ SRC /main/ Java/Stores all source code of the project. / SRC //main/resources/ stores all resource files and configuration files of the project. / SRC /test/ stores test code

Automatically generated file interpretation

The LessonOneApplication class contains a main method to run the project application. Properties file is the core configuration file of the project. There is a POM.xml Maven configuration file in the form of XX.xx.xx. Because we are using the Maven management JAR, a POM file is automatically generated here

@ SpringBootApplication annotations

We can see that the tool automatically generates an entry for us with an annotation @SpringBootApplication. What does it mainly do? It’s easy to see that this is the entry class that declares the current class to be Sprinboot. Only one such annotation exists within a SpringBoot project.




Figure 7.

Initial attempt to run the project

Right-click on the entry program class ->Run LessonOneApplication on the console output as shown in Figure 8, proving that you have successfully Run the SpringBoot project.




Figure 8.

As you can see, our project was started using the built-in Tomcat container and the default port number is 8080 (updated in a later article on how to change the port number). We now get no response from our browser, except for an error page, as shown in “Figure 9” below




Figure 9.

Write HelloWordController

We first in/SRC/main/Java/com/yuqiyu (the directory you build the project directory) to create a directory called lessonOne package, and then under lessonOne create a package called the controller, Finally we create a helloWordController.java class file under the Controller package, and we use @restController to declare that our newly created HelloWordController is an access controller, as shown in “Figure 10” below:




Figure 10.

You can see that I configured a @requestMapping annotation to declare that the index() method can be accessed through 127.0.0.1:8080/index, so let’s restart the project (if there is an error in the restart of the project it should be an annotation configuration problem) and access the address as shown in “Figure 11” :




Figure 11.

You can see that we output “HelloWord”! And that completes our chapter.

The source address

I will share the source address of this chapter and the following chapters, please download and git Clone yourself.

SpringBoot source code address: gitee.com/hengboy/spr…

SpringCloud source code address: gitee.com/hengboy/spr…

For SpringBoot related articles, visit: Directory: SpringBoot learning directory

For a series of QueryDSL articles, visit the QueryDSL Generic Query Framework learning directory

For articles in the SpringDataJPA series, visit: Directory: SpringDataJPA Learning directory

SpringBoot related articles please visit: directory: SpringBoot learning directory, thank you for reading!

conclusion

This chapter mainly through springboot simple features to complete the first “HelloWord” Web application construction, we can see that Springboot has been very mature in building a project integration component, recommend you to use the framework in future projects, It can effectively improve the team development efficiency and shorten the development cycle. I am the first time to write an article, please bear with me, if there is any improvement, please point out, thank you very much!

Welcome to join QQ technical exchange group, common progress.




QQ technical exchange group