SpringBoot快速入门

SpringBoot快速入门

从使用Maven零搭建一个SpringBoot项目

  1. 创建maven工程

    无需勾选 archetype,直接点击下一步即可

    输入坐标,项目名点击下一步,完成后选择AutoImport即可

    image-20191025161336674

  2. 添加SpringBoot依赖

    这里只是对父工的引用,没有实际的引用,但是做了版本的声明,在加入依赖后无需指定版

    	<parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.3.RELEASE</version>
        </parent>
    
  3. 添加SpringBoot启动器依赖

    	<dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>
    添加启动器后 web工程常用的依赖会自动帮你引入
    
  4. 编写启动类

    package cn.rayfoo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class);
        }
    }
    
    
  5. 编写Controller直接访问

    package cn.rayfoo.web;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class UserController {
    
        @RequestMapping("/findOne")
        public String getOne(){
            return "ok";
        }
    
    }
    
    
  6. 配置SpringBoot热部署

             <dependency> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>
    

补充:如果遇到run启动非常缓慢解决方法如下:

​ 1.在命令行中输入hostname 查询当前主机名称
​ 2.到C盘WindowsSystem32driversetc中找到host文件
​ 3.复制一份其它地方进行编辑,编辑时在hostname之后添加.local
​ 4.注意事项: 127.0.0.1和local之间是两个tab 不是空格

原文地址:https://www.cnblogs.com/zhangruifeng/p/11898025.html