spring boot入门案例

179 阅读1分钟

概述

  • 什么是 spring boot ?

    • 用来简化新Spring应用的初始搭建以及开发过程
  • 特点:

    • 维护依赖,解决jar冲突。提供众多启动器 ....-starter (相当于jar包集合)
    • 简化配置,大大减少了配置文件、配置类。
    • 默认实现,默认情况下,提供了众多解决方案。

入门案例:手动搭建

分析

  • 创建maven项目
  • 在pom.xml文件,添加 web开发启动器
  • 编写controller,基于RESTFul
  • 编写启动类

实施

  • 创建maven项目:spring_boot_hello

  • 在pom.xml文件,添加 web开发启动器

        <!--确定spring boot版本-->
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.2.5.RELEASE</version>
            <relativePath/>
        </parent>
    
        <dependencies>
            <!--web开发启动器-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>
    
  • 编写controller,基于RESTFul

    package com.czxy.boot.controller;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    
    @RestController
    @RequestMapping("/hello")
    public class HelloController {
    
        @GetMapping
        public String hello() {
            return "你好";
        }
    }
    
  • 编写启动类

    package com.czxy.boot;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class HelloApplication {
        public static void main(String[] args) {
            SpringApplication.run(HelloApplication.class, args);
        }
    }
    

访问 在这里插入图片描述