Getting started with SpringBoot2.0 (detailed documentation)

What is Springboot

Spring Boot is an open source framework for developing Java applications. It is based on the Spring framework and simplifies the configuration and deployment process of Spring applications. Spring Boot reduces developers' workload through automatic configuration and provides a way to quickly build applications.

Springboot2.x dependency environment and version new feature description

Explain the concept of new version dependency environment and new features of Springboot2

  1. Depends on version jdk8 or above, Springboot2.x uses JKD8, because the underlying layer is Spring framework5.
  2. Install the latest version of maven, maven3.2 or above
  3. new features
  4. Translation tool: https://translate.google.cn/
  5. springbootGitHub address: http://github.com/spring-projects/spring-boot
  6. Springboot official documentation: http://spring.io/guides/gs/spring-boot

Why learn Springboot

From the advantages of springboot

  1. Simplified configuration: Spring Boot provides an automatic configuration function that can automatically configure applications based on project dependencies and environments. Developers do not need to manually configure a large number of XML files or annotations, and can quickly build the basic framework of the entire project.
  2. Rapid development: Spring Boot provides many out-of-the-box functions and extension libraries, which can greatly reduce development time and workload. Developers can use these features and libraries to quickly build applications without having to rewrite common code.
  3. Standalone: ​​Spring Boot applications can run as standalone executables without the need for an external web server. It has a built-in embedded web server (such as Tomcat, Jetty, etc.) and can run applications directly. This reduces deployment and operational complexity
  4. Easy to test: Spring Boot provides a series of testing tools and frameworks to facilitate unit testing, integration testing and end-to-end testing. Developers can use these tools to ensure the quality and stability of their applications.
  5. Ecosystem: Spring Boot is built on top of the Spring framework and can take full advantage of the rich ecosystem of the Spring framework. Developers can easily integrate other Spring components and third-party libraries to provide more flexible and scalable solutions.

From the perspective of future development trends

The future development trend of Spring Boot is to pay more attention to microservice architecture, cloud native applications, reactive programming, security enhancement, and better integration and development experience. These trends will help Spring Boot continue to develop and grow in the future, and further enhance its status and influence among developers.

development environment

development tools

  • JDK version
  • Spring Boot version
  • Maven version: 3.5.2

Plug-ins involved:

  • FastJson
  • Swagger2
  • Thymeleaf
  • MyBatis
  • Redis
  • ActiveMQ
  • Shiro
  • Lucence
  • YAML

Spring Boot development environment setup and project startup

jdk configuration

Use IDEA for development. The way to configure jdk in IDEA is very simple. Open File->Project Structure, as shown in the figure below.
Insert image description here

  1. Select SDKs
  2. Select the local jdk installation directory in the JDK home path
  3. Customize the name for jdk in Name

Construction of Spring Boot project

maven configuration

After creating the Spring Boot project, maven configuration is required. Open File->Settings, search for maven, and configure the local maven information. as follows
Insert image description here

IDEA quick build

In IDEA, you can quickly build a Spring Boot project through File->New->Project. As follows, select Spring Initializr, select the jdk we just imported in Project SDK, click Next, and get the project configuration information.
Group: Fill in the corporate domain name. This course uses com.itcodai.
Artifact: Fill in the project name. The project name of each lesson in this course is commanded by course+course number. Here, course01 is used.
Dependencies: You can add the dependency information required in our project. Add according to the actual situation. For this course, you only need to select Web.

maven creates project

Springboot is built on maven. We only need to create a maven project and import SpringBoot dependencies. There is
no need to select a prototype. Just enter
Insert image description here
the project name and click Finish.
Insert image description here

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

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

Insert image description here

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

//这是一个复合注解,通常情况下,我们会把启动类放到项目的顶级目录
//注意:SpringBootApplication中的@ComponentScan会默认扫描本类所在包一下的子包
@SpringBootApplication
public class DemoAplication {
    
    

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

Insert image description here

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

@RestController
public class HelloController {
    
    

    @RequestMapping("/")
    public String hello(){
    
    
        return "hello";
    }
}

Insert image description here
Re-run the main method to start the project, enter localhost:8080 in the browser, if you see "hello", then congratulations on the successful start of your project! Spring Boot is so simple and convenient! The default port number is 8080. If you want to modify it, you can use server.port in the application.properties file to manually specify the port, such as port 811.

server.port=811

Common annotations

The annotation @SpringBootApplication of the startup class
is a compound annotation. @SpringBootApplication=@SpringBootConfiguration+@EnableAutoConfiguration+@ComponentScan
@Configuration: @SpringBootConfiguration is marked on the class, which is equivalent to using the class as a <beans> in Spring's xml configuration file. Its function is: configure the Spring container (application context) @EnableAutoConfiguration
: turn on automatic configuration. Some automatic configuration classes of Spring.facotries under MATA-INF
@ComponentScan: scan annotations. If basepackage is not configured, all classes at the same level and subdirectories of the @ComponentScan annotated class will be scanned by default. So put the startup class in the top-level directory.
The annotations of the control layer
@RestController and @RequestMapping are SpringMvc annotations, not SpringBoot’s unique
@RestController = @Controller+@ResponseBody

Complete code

//bean类
import com.fasterxml.jackson.annotation.*;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;

@Data
public class Person implements Serializable {
    
    

    private String userid;

    @JsonIgnore//字段不返回
    private int age;

    @JsonFormat(pattern = "yyyy-MM-dd")//指定时间格式
    private Date time;

    @JsonProperty("dizi")//指定别名 注意:使用别名后,请求中key也要使用别名
    private String address;

    @JsonInclude(JsonInclude.Include.NON_NULL)//空字段不返回
    private String mailbox;


}
Controllerimport com.demo.bean.Person;
import org.springframework.web.bind.annotation.*;


@RestController
public class HelloController {
    
    

    @RequestMapping("/")
    public String hello(){
    
    
        return "hello";
    }

    /**
     * 功能描述:restful协议。从路径中获取参数
     * 1.接口中的参数定义,建议使用下划线隔开,不在使用驼峰
     * 2.path="/{cityid}/{userid}",标识路径中那些参数
     * 3.method=RequestMethod.GET,只处理get请求
     * 4.@PathVariable("cityid"),从路径中取出参数值
     * @return
     */
    @RequestMapping(value = "/{cityid}/{userid}",method = RequestMethod.GET )
    public Object test(
            @PathVariable("cityid")String cityid,
            @PathVariable("userid")String userid){
    
    
        return cityid+"---"+userid;
    }

    /**
     *参数的默认值设置
     * 添加@RequestParam 默认此参数是必填的
     * @return
     */
    @RequestMapping("/test02")
    public Object test02(@RequestParam(required = false,defaultValue = "2") Integer pageNo){
    
    
        return pageNo;
    }

    /**
     * 测试@RequestBody。要从请求体中获取数据
     * 有如下要求:
     * 1.数据要在请求体中
     * 2.数据的格式为content-type 是值是application/json
     * @return
     */
    @RequestMapping("/test03")
    public Object test03(@RequestBody Person person){
    
    
        return person;
    }

    /**
     * 从请求头获取数据
     * 一般情况下请求头中放置认证信息,例如access_token
     * @param access_token
     * @return
     */
    @RequestMapping("/test04")
    public Object test04(@RequestHeader String access_token){
    
    
        return access_token;
    }

    /**
     * 获取实体类的参数
     * @JsonIgnore//字段不返回
     * @JsonFormat(pattern = "yyyy-MM-dd")//指定时间格式
     * @JsonProperty("")//指定别名 注意:使用别名后,请求中key也要使用别名
     * @JsonInclude(JsonInclude.Include.NON_NULL)//空字段不返回
     * @return
     */
    @RequestMapping("/test05")
    public Object test05(@RequestBody Person person){
    
    
        return person;
    }
}
//pom.xml
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.5</version>
    </parent>

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

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.5.1</version>
        </dependency>
        
    </dependencies>

Insert image description here

Guess you like

Origin blog.csdn.net/m0_65491952/article/details/132558260