Spring boot quick start HelloWorld case

After reading the introductory case of Dark Horse, based on my own understanding, I will summarize it myself

Table of contents

(Suggestion) Change IDEA's maven mirror source

New Empty Project

Creation method one:

Creation method two:

Creation method three:

Creation method four:


My project environment

JDK8

IDEA 2021.2.3


(Suggestion) Change IDEA's maven mirror source

Maven is a project management tool, we may use the jar package provided by maven when we write programs

Maven's central warehouse is abroad, and downloading from abroad will be much slower

In addition, we need to download a large number of jar packages from the maven warehouse to build a spring project

Many domestic organizations synchronize maven packages on a regular basis, so our downloads from domestic sources will be much faster

Come to this directory of IDEA

Can be opened with Notepad or a text editor

down to find

Add such a paragraph under mirrors

<mirror>
    <id>aliyunmaven</id>
    <mirrorOf>central</mirrorOf>
    <name>aliyunmaven</name>
    <url>https://maven.aliyun.com/repository/public</url>
</mirror> 


New Empty Project

Note: keep the network open

Create an empty project before starting

There will be several modules that we will write later

 click finish


Creation method one:

        Advantage of Method 1: Based on the official website creation, it can ensure that the project template is up-to-date and the file standard

steps:

  •  Enter the main interface, create a new spring boot module

  • Fill in the project information

  • choose to depend

The role of shared index: 

quote answer

The latest version of idea2020.3 always prompts what this means-CSDN Community

 PS:

Since I have downloaded it from the maven warehouse before, this time the construction project is completed directly, and it usually takes about 2-3 minutes to download

Wait patiently for the progress bar in the lower right corner

  •  The project structure at this time

  • New package controller

Note that it must be created in the same directory as Demo1Application (that is, under canyue)

  • The controller package creates a new class Hello

Write the following content (the note here actually refers to Mad God, it feels simpler?)

package com.canyue.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author mobeiCanyue
 * Create:  2022-03-04 21:24
 * Describe:
 */
@RestController
public class Hello {
    @RequestMapping("/hello")   //注意,这里就是一会要访问的虚拟路径
    public String hello() {
        return "Hello World !!!";
    }
}

  • start up! 

  • If you see the spring LOGO and no error is reported, the startup is successful

  • Open the browser and enter the virtual path in the comment just now

  •  The browser successfully displays the message we returned


Creation method two:

        The advantage of method 2: download offline templates from the official website, and the templates can be used offline 

 

  • open url

Spring Initializrhttps://start.spring.io/

  • Fill in the relevant information of the template 

  • Select the spring web module

 

  • Click to pack and download

Download completed

copy the folder 

  • This folder, right click and paste it under our project

  • Note that open with the file explorer, do not copy directly to IDEA

Don't close that project just now

  • Create a new module from an existing module

  • select demo2

 

  • Select the type as maven

  • Come to the main interface, select the project structure

Change the JDK (make sure it is 8)

 just say the same        

  • Just like before, create a new package

  • Change the virtual path and output content 

 

  • run

Creation method three:

        Method 3 advantages: Alibaba Cloud, domestic server, faster download speed

                             But the pom.xml file will be different from the official website

 

  •  The server to replace the initial template is:

https://start.aliyun.com/

  • same as method one

  • Wait for the package to download (here also wait)

  • Modify the pom.xml file

  • Refresh maven to load dependencies

  • Make a Hello in the same way
  • Change the virtual path to 3 and change the output to 3

Creation method four:

        Advantages of Method 4: Manually created by Maven, there are no redundant files and can be created without network connection (similar to Method 2)

  • Fill in the module information

  • The structure of the project at this point

  • Now let's add and change the content of pom.xml, the content is as follows

<?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>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.4</version>
    </parent>

    <groupId>com.canyue</groupId>
    <artifactId>demo4</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    
</project>

Refresh the maven configuration file

Imitate the previous demo and write a springApplication

package com.canyue;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author mobeiCanyue
 * Create:  2022-03-05 0:46
 * Describe:
 */
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }
}

 Create the hello class in the same way

runApplication

Guess you like

Origin blog.csdn.net/m0_52559040/article/details/123284034