As the number of readers is increasing, it is inevitable that I will be asked some questions I feel embarrassed about. For example, Xiao Wang asked me a few days ago: “Brother, please teach me how to create a Hello World project through Spring Boot?”

I was shocked when I received the question! What year, still can’t use Spring Boot, big Qing dynasty early died ah!

Before I complained, Xiao Wang immediately said: “Brother, don’t get angry. I submitted 14 resumes before I found an internship in a small company in a third-tier town. It was not easy. I heard of erge hot intestine ancient road, I hold the mentality of trying to add your friend.

At this point, my anger naturally dissipated. After that, I spent five minutes helping him out. Unexpectedly, he even sent me a small red envelope to show his gratitude to me. And suggested that I write an article, because he thinks there are many white people like him. At the beginning, I was a little hesitant, after all, there have been a lot of articles about Spring Boot on the Internet, and it’s still “Hello World”. But on second thought, it is our duty as writers to have even one reader. And so you have this article.

00. Introduction to Spring Boot

I’m guessing you’ve written spring-based applications, and you know that a “Hello World” requires a lot of configuration. Come to think of it, I’m a little skeptical that I can create it, especially those XML files, which are completely unprintable.

Spring Boot makes it easy to create stand-alone, production-grade, Spring-based applications that run directly. We have our own ideas about the Spring platform and third-party libraries, so you’ll have minimal trouble getting started.

Look at the Spring Boot website for “bragging about yourself”, and it feels really good. This means we can quickly create a working Spring application with very little configuration. And these few configurations are annotated, not XML.

In summary, Spring Boot is a lightweight framework that does most of the configuration work for Spring-based applications.

01. Create a Spring Boot project using Spring Initlallzr

Creating a Spring Boot project is as simple as Spring Initlallzr (start.spring.io/). (Actually, I just threw the url at Xiao Wang.)

1) The first option is usually Maven (the preferred option for Java back-end projects). Gradle is commonly used in Android projects. If you don’t know much about Maven, check out my previous article, Getting Started with Maven.

2) The second option is usually Java.

3) The third option defaults to 2.2.2, the most stable version of Spring Boot currently available.

4) In the fourth option, fill in the path and name of the project.

5) For the fifth option, we choose Spring Web and Spring Boot Actuator, indicating that the project is a Web project; The Actuator integrates introspection and monitoring of application systems provided by Spring Boot. It can view detailed information about application configurations, such as automatic configuration information, created Spring Beans, and some environment properties.

After selecting the options, you can click on the [Generate] button to Generate an initial Spring Boot project. The generated package is a compressed package that needs to be unpacked when imported into the IDE.

02. Import the Spring Boot project into IDEA

I’ve been getting into IDEA lately, so I’m not using Eclipse anymore. If you’re really uncomfortable with IDEA, you can opt for Spring’s own IDE, STS, which is eclipse-based.

PS: The import process is omitted, please select Maven.

Wait for Maven to download all dependencies.

The directory structure diagram for the project is shown below.

1) HelloSpringBootApplication for entry of the project, with the main () method. As we know, traditional Web projects typically need to be launched and run under a container such as Tomcat, but the main() method implies that the project can be run directly as a JAR package — Tomcat is already built into Spring Boot.

2) The @SpringBootApplication annotation is very interesting. Its source code is as follows:

@Target({ElementType.TYPE})

@Retention(RetentionPolicy.RUNTIME)

@Documented

@Inherited

@SpringBootConfiguration

@EnableAutoConfiguration

@ComponentScan(

    excludeFilters = {@Filter(

    type = FilterType.CUSTOM,

    classes = {TypeExcludeFilter.class}

), @Filter(

    type = FilterType.CUSTOM,

    classes = {AutoConfigurationExcludeFilter.class}

)}

)

Copy the code

From these annotations, we can roughly see that the Spring Boot project uses a lot of annotations instead of cumbersome XML configuration.

03. Edit the Spring Boot project

Take a look at this code.

@SpringBootApplication

@RestController

public class HelloSpringBootApplication {



    public static void main(String[] args) {

        SpringApplication.run(HelloSpringBootApplication.class, args);

    }



    @RequestMapping("hello")

    public String hello(a) {

        return "Hello World";

    }

}

Copy the code

1) The @RestController annotation is equivalent to @responseBody + @Controller, which means that the requested object can be returned directly in JSON format.

2) The hello() method is very simple and returns a string “Hello World”. RequestMapping indicates that this method is a RequestMapping.

04. Run the Spring Boot project

Next, we run directly HelloSpringBootApplication class, such a Spring Boot project started successfully.

The default port number is 8080.

In this case, you can directly test whether the project is successfully started in the Terminal panel of IDEA.

The curl command behavior at http://localhost:8080/hello. The project path helloSpringBoot is not required because the default startup is equivalent to a ROOT level, so there is no need for a ROOT directory.

Curl is a common command-line tool used to request Web servers. Its name is a combination of client and URL. Curl is a powerful tool with dozens of command-line arguments. If used skillfully, it can completely replace Postman.

05. Run the Spring Boot project in JAR form

When we open pom.xml, we see the following:

<build>

    <plugins>

        <plugin>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-maven-plugin</artifactId>

        </plugin>

    </plugins>

</build>

Copy the code

This means that we can package Spring Boot projects into JAR files using the Maven command MVN Clean Package.

You can also add arguments to skip testing at packaging time: MVN clean package-dmaven.test.skip

After a while, you can see the corresponding JAR package in the target directory. This JAR package has an advantage over the traditional WAR package because there is no need to open a separate container to run projects, which Spring Boot has built in. The native jar file (.jar. Original) is less than 3 KB, which is very small because there are few lines of code, but when packaged, the.jar file is 19 megabytes, which shows that Spring Boot does a lot of work for us that we can’t see.

Run the Java -jar helloSpringboot-0.0.1 -snapshot. jar command directly to run the jar package, you can also see the Tomcat startup information.

This time, let’s use a browser to access it.

Also OK, which means our first Spring Boot program is running. Give yourself a thumbs up.

06, thanks

Well, dear readers, the article promised to Xiao Wang is finally finished. Can see here are the most excellent programmers, promotion pay is you 👍. In this paper, the source of form a complete set has been uploaded to making [SpringBootDemo. HelloSpringBoot].

If you find this article helpful, please search “Silent King ii” on wechat and read it for the first time.

GitHub, portal ~, GitHub, portal ~, GitHub, Portal ~

I am the Silent King 2, a programmer with good looks but mediocre talent. Attention can improve learning efficiency, don’t forget three even ah, like, collect, message, I don’t pick, hee hee.