Introduction to SpringBootQuick Start Get started with SpringBoot Use IDEA to build SpringBoot projects

Introduction to Spring Boot

Introduction

1. What is Spring Boot?
Spring is a new generation of JavaEE development standards that provide us with a convenient development method.
Spring Boot is a sub-project under the Spring open source framework. It is a one-stop solution for Spring. It mainly simplifies the difficulty of using Spring and reduces the need for configuration files. Requirements , making it easier for developers to get started.
2. Why use Spring Boot?
SpringBoot has many advantages

  • 1. Simplified the Spring configuration file,
  • 2. No code and XML file generation
  • 3. Built-in TomCat
  • 4. Ability to run independently
  • 5. Simplify monitoring
  • n.

Quick start

Use IDEA to quickly create projects

  1. Open IDEA tool and select new project
  2. Choose Springinitialzr to quickly create SpringBoot applications

image

  1. The creation process may be slower depending on the network

image

  1. Fill in the relevant information to create the project
  • groupId your company's domain name reverse
  • artifactId your project name
  • The version number of the version project is 1.0.0 or 0.0.1.
  • description The description of the item
  • package package name

image

  1. After filling in, click Next => finish, wait a moment
  • It may take a long time to create for the first time
  1. Wait for the loading below to see the complete project structure on the left
    image
  2. This xxxxApplication.java is the main class of SpringBoot application

image
8. Add the following code inside

package cn.yufire.springbootdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class SpringbootDemoApplication {
    
    

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

    @GetMapping("/hello")
    public String helloSpringBoot(){
    
    
        return "hello, world";
    }
    
}

  1. Click the start button in the upper right corner or the start button to the left of the project class name

image

  1. Console output

Console output

  1. Open the browser and visit
    localhost:8080/hello

image

The pom.xml file after the project is created

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!--SpringBoot父依赖 版本号都在里边-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <!--SpringBoot的版本-->
        <version>2.2.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <!--组织ID  一般是你的域名 或你公司的域名 反转-->
    <groupId>cn.yufire</groupId>
    <!--项目名称-->
    <artifactId>springboot-demo</artifactId>
    <!--项目的版本号-->
    <version>1.0.0-SNAPSHOT</version>
    <!--项目名称-->
    <name>springboot-demo</name>
    <!--项目的描述-->
    <description>Demo project for Spring Boot</description>

    <!--java版本-->
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <!--所有的Maven依赖-->
    <dependencies>
        <!--Spring-boot-starter-Web模块的依赖 实现Web的主要模块-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--Spring-Boot的测试模块-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <!--使用Maven插件进行构建项目-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>


Entertainment Banner.txt is useless

The role of Banner.txt:

  • 1. You can display something when the Spring Boot project starts
    • 1.1. SpringBoot version information
    • 1.2 can display a custom character painting Custom Banner
    • 1.3. Wait
${AnsiColor.BRIGHT_RED}

//                          _ooOoo_                               //
//                         o8888888o                              //
//                         88" . "88                              //
//                         (| ^_^ |)                              //
//                         O\  =  /O                              //
//                      ____/`---'\____                           //
//                    .'  \\|     |//  `.                         //
//                   /  \\|||  :  |||//  \                        //
//                  /  _||||| -:- |||||-  \                       //
//                  |   | \\\  -  /// |   |                       //
//                  | \_|  ''\---/''  |   |                       //
//                  \  .-\__  `-`  ___/-. /                       //
//                ___`. .'  /--.--\  `. . ___                     //
//              ."" '<  `.___\_<|>_/___.'  >'"".                  //
//            | | :  `- \`.;`\ _ /`;.`/ - ` : | |                 //
//            \  \ `-.   \_ __\ /__ _/   .-` /  /                 //
//      ========`-.____`-.___\_____/___.-`____.-'========         //
//                           `=---='                              //
//      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^        //
//            佛祖保佑       永不宕机     永无BUG                  //

-----版本号-----${spring-boot.version}

SpringBoot configuration file priority

  • /Config/application.* in the config directory under the project root path has the highest priority
  • /Application.* under the project root path has the second priority
  • /Src/main/resources/config/application.* in the config directory under the resources directory of the project has the third priority
  • /Src/main/resources/application.* under the resources directory of the project has the lowest priority

JSR303 data verification

When the user fills in the form on the front-end page, the front-end js program will verify the validity of the parameters.
When the data reaches the back-end, in order to prevent malicious operations and maintain the robustness of the program, the
back-end also needs to verify the data

JSR303
is a set of JavaBean parameter verification standards, which defines many commonly used verification annotations.
These annotations can be directly added to the attributes of our JavaBeans, and
verification can be performed when verification is needed.

  1. First add an annotation on the JavaBean that needs to be verified
@Validated  //JSR-303校验
  1. Add annotations to the attributes that need to be verified
    2.1 Commonly used annotations of JSR303 Commonly used annotation addresses of JSR303
    @NotNull(message = "姓名不能为空")
    private String name;
    @Min(value = 0 ,message = "年龄不能小于0岁")
    private Integer age;
  1. Modify the configuration in yml and set the name attribute to empty
  2. Test result
    [External link image transfer failed, the source site may have an anti-leech link mechanism, it is recommended to save the image and upload it directly (img-znEwIdD6-1588560141805)(BB9FD67E0FEB464491C7DFEBC1903D22)]

Exclude a specific configuration

Add to the configuration in the SpringBoot startup class

@SpringBootApplication(exclude = {
    
    DataSourceAutoConfiguration.class})

Common configuration of Spring Boot


server
    port: 8080 # 配置Tomcat启动端口
    servlet:
        context-path: /boot #配置上下文 就是在访问地址前加上这个字符串

Guess you like

Origin blog.csdn.net/weixin_43420255/article/details/105914315