preface

Spring Boot benefits needless to say, for those who want to work in Java, it is a must learn skills.

In my opinion, its advantage is how fast and cheap.

  • Many functions, many commonly used capabilities are integrated;
  • Access fast, simple a few lines of code can complete access;
  • Good maintenance, high community activity, problems can be found in the first time to answer;
  • Save money, free and open source projects are the natural favorites of individuals and businesses.

Next, I will elaborate on the Spring Boot access tutorial, which is convenient for readers who just contact this technology to learn.

Related,

Technology stack:

  • Java 8
  • Maven 3
  • Spring Boot 2.4.2

Environment:

  • Operating system: MacOS (Windows is similar)
  • IDE (Integrated Development Environment) : IntelliJ IDEA flagship version 2020.1

The Spring Boot project is initialized

There are two ways:

  1. Official website of packaging
  2. IDE to generate

Official website of packaging

Go to start.spring. IO /.

Specify how the project is built, Maven, and select the language Java.

Spring Boot selects the latest stable version 2.4.2, and fills in the organization and project-related information in the project metadata.

On the right, I rely on the packages I will use. Here, I choose Lombok, which helps us write less code, and Spring Web, which supports Web page access for my demonstration.

Then click the GENERATE button at the bottom and the site will automatically GENERATE the ZIP package and download it locally.

When unzipped, we should see the following directory structure. HELP is the HELP document, MVNW is the program launch command file, POM places the project dependencies, and SRC contains the project source code.

IDE to generate

Access plug-in:

  • Go to the preferences of IntelliJ IDEA, search for Spring Boot in Plugins, and tick the box to make the plugin take effect.
  • Restart the IDE as prompted.

Enter IntelliJ IDEA and click Create new project.

Select Spring Initializr, specify the Java SDK for the project, select the default startup method, which requires networking, and click Next.

By default, fill in the following information, as shown in the figure below:

I changed the name to course to indicate that it was a project on the tutorial site. I also dropped the default Java version to 8, which I use more often.

Click next, and you’ll see a page for adding dependencies, which supports keyword search and allows you to select the dependency packages you want. The actual effect is the same as the official page, so I still choose Lombok and Spring Web.

Click Next and set the project name and path. The default name is the artifact value from the previous Maven setup, which I will not change.

Click Finish and the IDE will help us build the Spring Boot project. The file structure is the same as the package downloaded from our official website.

The project will generate an application launch portal for us, select the main method to run, and the Spring Boot program will run.

Here is the startup log, with the red square in the upper right representing running. Click the red square again, the console will prompt the program to exit, the upper right square will also become gray.

In addition to starting Spring Boot with the IDE, you can also use the command-line tools generated by the project. Locate the project’s home directory and run the following command (MacOS/Linux) :

 ./mvnw spring-boot:run
Copy the code

Windows users run the following command:

mvnw spring-boot:run
Copy the code

You’ll see the Spring Boot project from build to compile to deploy.

If you want to terminate the application, Ctrl + C.

The demo didn’t touch on Web pages, so what do you do if you want to see what the page looks like?

Adding Web code

Web-related dependencies were already introduced when we were building the project, so we just need to add the related code.

First locate the file CourseApplication.java, then add the annotation @RestController to the startup class, along with a Web access method.

@SpringBootApplication
@RestController
public class CourseApplication {

    public static void main(String[] args) {
        SpringApplication.run(CourseApplication.class, args);
    }

    @GetMapping("/hello")
    public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
        return String.format("Hello %s!", name); }}Copy the code

At this time to start the Spring the Boot, and then open the browser to access http://localhost:8080/hello * * * *, you can see the default output. Why is port 8080? If you go back and look at the screenshot of the startup log, you will see that the project uses a Tomcat Web server and the HTTP initialization port is 8080.

It is also possible to access the Get request with parameters, such as the following.

The port can also be specified by placing it in the application configuration file application.properties.

Port =9090 server.port=9090Copy the code

After the program is restarted, log printing has changed, and browser access can be successful only by using the newly configured port.

If it is useful to you, please give me a “like”, click “Watch”, or share it with your friends. Thank you very much.

I’m a snail and I’m crawling around the Internet. See you next time.